<?php
/**
 * Color Control
 *
 * Uses WordPress Color Picker (Iris)
 *
 * @package GuardDog
 * @since 1.8.45
 */

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

class Guard_Dog_Color_Control extends Guard_Dog_Customizer_Control {

    /**
     * Render the control
     *
     * @param mixed $value Current value
     */
    public function render($value) {
        $this->render_wrapper_start();
        ?>
        <input type="text"
               id="<?php echo esc_attr($this->id); ?>"
               name="<?php echo esc_attr($this->id); ?>"
               class="guard-dog-color-picker"
               value="<?php echo esc_attr($value); ?>"
               data-default-color="<?php echo esc_attr($this->get_default()); ?>"
               <?php echo $this->get_input_attrs_html(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
        />
        <?php
        $this->render_wrapper_end();
    }

    /**
     * Sanitize the control value
     *
     * @param mixed $value Value to sanitize
     * @return string Sanitized hex color
     */
    public function sanitize($value) {
        // Handle empty value
        if (empty($value)) {
            return $this->get_default();
        }

        // Sanitize hex color
        $value = sanitize_hex_color($value);

        // Fall back to default if invalid
        if (empty($value)) {
            return $this->get_default();
        }

        return $value;
    }

    /**
     * Validate the control value
     *
     * @param mixed $value Value to validate
     * @return bool True if valid hex color
     */
    public function validate($value) {
        if (empty($value)) {
            return true; // Allow empty to use default
        }

        return (bool) sanitize_hex_color($value);
    }
}
