<?php

if (!defined('ABSPATH')) {
    exit;
}

/**
 * Resolve a frontend template path under public/templates/.
 */
function iapc_get_frontend_template_path($name) {
    return IAPC_PUBLIC_DIR . 'templates/' . ltrim((string) $name, '/');
}

/**
 * Load a frontend template with optional extracted variables.
 */
function iapc_load_frontend_template($name, array $vars = array()) {
    $path = iapc_get_frontend_template_path($name);

    if (!is_readable($path)) {
        return;
    }

    if ($vars) {
        extract($vars, EXTR_SKIP);
    }

    include $path;
}

/**
 * Shared attribute map for interactive designer controls.
 *
 * @return array<string, bool>
 */
function iapc_kses_designer_common_attrs() {
    return array(
        'id'              => true,
        'class'           => true,
        'style'           => true,
        'title'           => true,
        'role'            => true,
        'tabindex'        => true,
        'aria-hidden'     => true,
        'aria-label'      => true,
        'aria-labelledby' => true,
        'aria-disabled'   => true,
        'aria-modal'      => true,
        'aria-live'       => true,
        'aria-busy'       => true,
        'hidden'          => true,
        'data-panel'             => true,
        'data-panel-content'     => true,
        'data-base-price'        => true,
        'data-no-base-price'     => true,
        'data-group'             => true,
        'data-value'             => true,
        'data-iapc-close-guided' => true,
        'data-handle'            => true,
        'data-target'            => true,
        'data-preview'           => true,
        'data-product_id'        => true,
        'data-product_sku'       => true,
        'data-quantity'          => true,
        'data-view'              => true,
    );
}

/**
 * Sanitize designer markup while preserving required tags (canvas, forms, controls).
 * Used for shortcode returns and product-page echo (WP.org escaping requirement).
 *
 * @param string $html Raw designer HTML.
 * @return string
 */
function iapc_kses_designer_html($html) {
    $allowed = wp_kses_allowed_html('post');
    $common  = iapc_kses_designer_common_attrs();

    $allowed['section'] = $common;
    $allowed['div']     = isset($allowed['div']) && is_array($allowed['div'])
        ? array_merge($allowed['div'], $common)
        : $common;
    $allowed['span']    = isset($allowed['span']) && is_array($allowed['span'])
        ? array_merge($allowed['span'], $common)
        : $common;
    $allowed['p']       = isset($allowed['p']) && is_array($allowed['p'])
        ? array_merge($allowed['p'], $common)
        : $common;
    $allowed['h2']      = isset($allowed['h2']) && is_array($allowed['h2'])
        ? array_merge($allowed['h2'], $common)
        : $common;
    $allowed['h3']      = isset($allowed['h3']) && is_array($allowed['h3'])
        ? array_merge($allowed['h3'], $common)
        : $common;
    $allowed['ul']      = isset($allowed['ul']) && is_array($allowed['ul'])
        ? array_merge($allowed['ul'], $common)
        : $common;
    $allowed['li']      = isset($allowed['li']) && is_array($allowed['li'])
        ? array_merge($allowed['li'], $common)
        : $common;
    $allowed['a']       = isset($allowed['a']) && is_array($allowed['a'])
        ? array_merge($allowed['a'], $common)
        : $common;
    $allowed['strong']  = isset($allowed['strong']) && is_array($allowed['strong'])
        ? array_merge($allowed['strong'], $common)
        : $common;
    $allowed['details'] = array_merge($common, array('open' => true));
    $allowed['summary'] = $common;
    $allowed['hr']      = $common;

    $allowed['button'] = array_merge(
        $common,
        array(
            'type'     => true,
            'name'     => true,
            'value'    => true,
            'disabled' => true,
        )
    );

    $allowed['label'] = array_merge(
        $common,
        array(
            'for' => true,
        )
    );

    $allowed['form'] = array_merge(
        $common,
        array(
            'action'       => true,
            'method'       => true,
            'enctype'      => true,
            'novalidate'   => true,
            'autocomplete' => true,
        )
    );

    $allowed['input'] = array_merge(
        $common,
        array(
            'type'         => true,
            'name'         => true,
            'value'        => true,
            'placeholder'  => true,
            'min'          => true,
            'max'          => true,
            'step'         => true,
            'maxlength'    => true,
            'accept'       => true,
            'disabled'     => true,
            'checked'      => true,
            'readonly'     => true,
            'required'     => true,
            'autocomplete' => true,
            'inputmode'    => true,
            'size'         => true,
        )
    );

    $allowed['select'] = array_merge(
        $common,
        array(
            'name'     => true,
            'disabled' => true,
            'required' => true,
            'multiple' => true,
        )
    );

    $allowed['option'] = array(
        'value'    => true,
        'selected' => true,
        'disabled' => true,
        'label'    => true,
    );

    $allowed['textarea'] = array_merge(
        $common,
        array(
            'name'        => true,
            'rows'        => true,
            'cols'        => true,
            'placeholder' => true,
            'disabled'    => true,
            'readonly'    => true,
            'required'    => true,
            'maxlength'   => true,
        )
    );

    $allowed['canvas'] = array_merge(
        $common,
        array(
            'width'  => true,
            'height' => true,
        )
    );

    $allowed['svg'] = array_merge(
        $common,
        array(
            'width'       => true,
            'height'      => true,
            'viewbox'     => true,
            'fill'        => true,
            'xmlns'       => true,
            'focusable'   => true,
        )
    );

    $allowed['path'] = array(
        'd'               => true,
        'fill'            => true,
        'stroke'          => true,
        'stroke-width'    => true,
        'stroke-linecap'  => true,
        'stroke-linejoin' => true,
        'class'           => true,
    );

    /**
     * Filter allowed HTML for designer markup sanitization.
     *
     * @param array  $allowed Allowed tags/attributes for wp_kses().
     * @param string $html    Raw HTML being sanitized.
     */
    $allowed = apply_filters('iapc_kses_designer_allowed_html', $allowed, $html);

    return wp_kses((string) $html, $allowed);
}
