<?php

/**
 * Base for tabbed admin pages.
 *
 * @package northmill-online-shared
 *
 * @phpcs:disable Squiz.Commenting.FunctionComment.MissingParamTag
 */
declare (strict_types=1);
namespace Northmill_Online_Shared_Checkout\Northmill_Online_Shared\Modules\Admin\Pages;

use Northmill_Online_Shared_Checkout\Northmill_Online_Shared\Abstract_Shared_Base;
use Northmill_Online_Shared_Checkout\Northmill_Online_Shared\Modules\Assets\Asset_Location_Enum;
defined('ABSPATH') || exit;
/**
 * Class Abstract_Tabbed_Admin_Page
 */
abstract class Abstract_Tabbed_Admin_Page extends Abstract_Admin_Page
{
    /**
     * Get the page params. This is including the sections and the menu HTML.
     */
    public function get_page_params(): array
    {
        $tabs = $this->get_tabs();
        $menu_html = $this->prepare_tabs_and_menu($tabs);
        return array('tabs_html' => Abstract_Shared_Base::base_instance()->template_loader->get_admin_template_part('_general/tabs', array('tabs' => $tabs, 'menu_html' => $menu_html), \false, \true));
    }
    /**
     * Get the tabs for the page.
     *
     * @return array
     */
    final protected function get_tabs(): array
    {
        $tabs = $this->get_shared_tabs();
        return apply_filters('northmill_online_shared_admin_' . $this->page_id . '_tabs_' . Abstract_Shared_Base::base_instance()->package_info->get_service_name_underscored(), $tabs);
    }
    /**
     * Get any predefined Shared tabs.
     *
     * @return array
     */
    abstract protected function get_shared_tabs(): array;
    /**
     * Render content for status page tab.
     *
     * @param string $template_part Template part.
     * @param array  $params        Parameters to use in template file.
     *
     * @return string
     */
    protected function render_tab_content(string $template_part, array $params = array()): string
    {
        return Abstract_Shared_Base::base_instance()->template_loader->get_admin_template_part($this->page_id . '/tab-' . $template_part, $params, \false, \true);
    }
    /**
     * Prepare sections and menu.
     * Set the active section and prepare the menu.
     *
     * @param array $sections Sections to prepare. By reference.
     *
     * @return string The menu HTML.
     */
    protected function prepare_tabs_and_menu(array &$sections): string
    {
        $active_found = \false;
        foreach ($sections as &$section) {
            if (!isset($section['content']) && isset($section['template_part'])) {
                if (isset($section['view_params'])) {
                    if (is_callable($section['view_params'])) {
                        $section['view_params'] = $section['view_params']();
                    }
                    $section['content'] = $this->render_tab_content($section['template_part'], $section['view_params']);
                } else {
                    $section['content'] = $this->render_tab_content($section['template_part']);
                }
            }
            $section_active = $section['active'] ?? \false;
            if ($section_active) {
                if ($active_found) {
                    $section['active'] = \false;
                } else {
                    $active_found = \true;
                }
            } else {
                $section['active'] = \false;
            }
        }
        unset($section);
        if (!$active_found) {
            $sections[key($sections)]['active'] = \true;
        }
        return Abstract_Shared_Base::base_instance()->template_loader->get_admin_template_part('_general/menu', array('sections' => $sections), \false, \true);
    }
    /**
     * Determines if the page should be shown in the menu.
     *
     * @return bool
     */
    public function show_in_menu(): bool
    {
        if (empty($this->get_tabs())) {
            return \false;
        }
        return \true;
    }
    /**
     * {@inheritDoc}
     */
    public function setup_page(): void
    {
        parent::setup_page();
        Abstract_Shared_Base::base_instance()->assets->enquer->register_style('general/tabs', Asset_Location_Enum::ADMIN, array(), array($this, 'is_this_admin_page'));
        Abstract_Shared_Base::base_instance()->assets->enquer->register_script('general/tabs', Asset_Location_Enum::ADMIN, array('jquery'), null, array($this, 'is_this_admin_page'));
    }
}
