<?php
/**
 * Render settings class.
 *
 * @package SaveToWishlist\Classes\Admin
 * @since 1.0.0
 */
namespace SaveToWishlist\Classes\Admin;

use SaveToWishlist\Abstracts\Singleton;
use SaveToWishlist\Helpers\Helper_Functions;
use SaveToWishlist\Classes\Admin\Settings\Tabs;
use SaveToWishlist\Classes\Admin\Settings\Controls;

/**
 * Class Render_Settings
 *
 * This class serves as the render settings class.
 */
class Render_Settings extends Singleton {

    /**
     * REST save URL.
     *
     * @var string
     */
    public $rest_save_url;

    /**
     * REST action URL.
     *
     * @var string
     */
    public $rest_action_url;

    /**
     * REST importer URL.
     *
     * @var string
     */
    public $rest_importer_url;

    /**
     * Helper functions.
     *
     * @var Helper_Functions
     */
    private $helper_functions;

    /**
     * Constructor.
     *
     * @since 1.0.0
     * @access public
     */
    public function __construct() {
        $this->rest_save_url     = rest_url( 'saveto-wishlist/v1/admin/settings/save' );
        $this->rest_action_url   = rest_url( 'saveto-wishlist/v1/admin/settings/action' );
        $this->rest_importer_url = rest_url( 'saveto-wishlist/v1/admin/importer' );

        $this->helper_functions = Helper_Functions::instance();
    }

    /**
     * Render the admin app for the settings page.
     *
     * @since 1.0.0
     * @access public
     */
    public function render_admin_app_settings() {
        echo '<div class="wrap"><div id="saveto-wishlist-admin-app-settings"></div></div>';
        echo wp_kses_post( apply_filters( 'stwlite_admin_app_after', '' ) );
    }

    /**
     * Allowed tags.
     *
     * @since  1.0.0
     * @access public
     * @return array
     */
    public function allowed_tags() {
        global $allowedposttags;

        $allowed_html = wp_kses_allowed_html( 'post' );

        $allowed_tags = array();
        if ( ! empty( $allowed_html ) && is_array( $allowed_html ) ) {
            $allowed_tags = array_keys( $allowed_html );
        } else {
            $allowed_tags = array_keys( $allowedposttags );
        }
        sort( $allowed_tags );

        // Filter to modify allowed tags.
        $allowed_tags = apply_filters( 'stwlite_admin_allowed_tags', $allowed_tags );

        return $allowed_tags;
    }

    /**
     * Allowed Attributes.
     *
     * @since  1.0.0
     * @access public
     * @return array
     */
    public function allowed_attrs() {
        global $allowedposttags;

        $allowed_html = wp_kses_allowed_html( 'post' );

        $allowed_attrs = array();
        if ( ! empty( $allowed_html ) && is_array( $allowed_html ) ) {
            // Only get the keys of the array.
            $allowed_keys  = $this->helper_functions->get_array_values( $allowed_html );
            $allowed_attrs = array_merge( ...array_values( $allowed_keys ) );
            $allowed_attrs = array_unique( $allowed_attrs );
        } else {
            $allowed_attrs = array_keys( array_merge( ...array_values( $allowedposttags ) ) );
        }
        sort( $allowed_attrs );

        // Filter to modify allowed attributes.
        $allowed_attrs = apply_filters( 'stwlite_admin_allowed_attrs', $allowed_attrs );

        return $allowed_attrs;
    }

    /**
     * Get the registered tab settings.
     *
     * @since 1.0.0
     * @access public
     * @return array
     */
    public function get_registered_tab_settings() {
        // Get the tabs.
        $default_tabs = $this->_default_tabs();
        $tabs         = apply_filters( 'stwlite_admin_setting_tabs', $default_tabs );

        // Get the controls.
        $default_controls = $this->_default_controls();
        $controls         = apply_filters( 'stwlite_admin_setting_controls', $default_controls );

        // Get data tables.
        $data_tables = apply_filters( 'stwlite_admin_data_tables', array() );

        return array(
            'tabs'       => $tabs,
            'controls'   => $controls,
            'dataTables' => $data_tables,
        );
    }

    /**
     * Get default tabs.
     *
     * @since  1.0.0
     * @access private
     *
     * @return array
     */
    private function _default_tabs() {
        $tabs = Tabs::instance()->tabs();

        // Filter to modify default tabs.
        $default_tabs = apply_filters( 'stwlite_admin_setting_default_tabs', $tabs );

        return $default_tabs;
    }

    /**
     * Get default controls.
     *
     * @since  1.0.0
     * @access private
     *
     * @return array
     */
    private function _default_controls() {
        $controls = Controls::instance()->controls();

        // Filter to modify default controls.
        $default_controls = apply_filters( 'stwlite_admin_setting_default_controls', $controls );

        return $default_controls;
    }
}
