<?php
/**
 * Checkbox Control
 *
 * Simple checkbox input field
 *
 * @package GuardDog
 * @since 1.8.45
 */

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

class Guard_Dog_Checkbox_Control extends Guard_Dog_Customizer_Control {

    /**
     * Render the control
     *
     * @param mixed $value Current value
     */
    public function render($value) {
        $this->render_wrapper_start();
        $checked = !empty($value) || $value === true || $value === 'true' || $value === '1' || $value === 1;
        ?>
        <label class="customizer-checkbox-wrapper">
            <input type="checkbox"
                   id="<?php echo esc_attr($this->id); ?>"
                   name="<?php echo esc_attr($this->id); ?>"
                   value="1"
                   <?php checked($checked, true); ?>
                   <?php echo $this->get_input_attrs_html(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
            />
            <span class="customizer-checkbox-label">
                <?php echo esc_html($this->get_arg('checkbox_label', $this->get_arg('label'))); ?>
            </span>
        </label>
        <?php
        $this->render_wrapper_end();
    }

    /**
     * Sanitize the control value
     *
     * @param mixed $value Value to sanitize
     * @return bool Sanitized boolean
     */
    public function sanitize($value) {
        return !empty($value) && ($value === true || $value === 'true' || $value === '1' || $value === 1);
    }

    /**
     * Get default arguments
     *
     * @return array
     */
    protected function get_default_args() {
        return array_merge(parent::get_default_args(), [
            'checkbox_label' => '', // Optional separate label for the checkbox itself
            'default' => false,
        ]);
    }
}
