<?php

/**
 * AuthorWP Kirki Customizer Setup
 */

function author_kit_custom_byline_customizer()
{


    // Section 1: Author Bio Box
    new \Kirki\Section('author_kit_custom_byline_section', [
        'title'       => esc_html__('Custom Byline', 'author-kit'),
        'panel'       => 'author_kit_customizer_panel',
        'priority'    => 10,
        'description' => esc_html__('Customize author bylines.', 'author-kit'),
    ]);


    // Cache the active template to minimize repeated calls
    static $active_template = null; // Use static to avoid redundant lookups
    if ($active_template === null) {
        $active_template = get_option('author_kit_custom_byline_template', 'byline-default');
    }

    // Define control classes mapping

    $control_classes = [
        'typography'   => \Kirki\Field\Typography::class,
        'color'        => \Kirki\Field\Color::class,
        'dimensions'   => \Kirki\Field\Dimensions::class,
        'dimension'    => \Kirki\Field\Dimension::class,
        'radio-button' => \Kirki\Field\Radio_Buttonset::class,
        'checkbox'     => \Kirki\Field\Checkbox_Switch::class,
        'select'       => \Kirki\Field\Select::class,
        'text'         => \Kirki\Field\Text::class,
        'slider'       => \Kirki\Field\Slider::class,
        'switch'       =>  \kirki\Field\Checkbox_Switch::class,
        // Pro Controls
        'divider'      => \Kirki\Pro\Field\Divider::class,
        'headline'     => \Kirki\Pro\Field\Headline::class,
        'margin'       => \Kirki\Pro\Field\Margin::class,
        'padding'      => \Kirki\Pro\Field\Padding::class,
    ];


    // Template controls definition
    $template_controls['byline-default']  = [
       
       // 1. Dimensions
       [
        'type'        => 'dimensions',
        'settings'    => 'author_kit_byline_dimensions_default',
        'label'       => esc_html__('Byline Box Dimensions', 'author-kit'),
        'description' => esc_html__('Set the width and height of the byline box.', 'author-kit'),
        'section'     => 'author_kit_custom_byline_section',
        'priority'    => 10,
        'default'     => [
            'width'  => '100%',
            'height' => 'auto',
        ],
        'transport'   => 'postMessage',
    ],
    
    // 2. Background Colors
    [
        'type'              => 'color',
        'settings'          => 'author_kit_byline_bg_color_default',
        'label'             => esc_html__('Background Color', 'author-kit'),
        'section'           => 'author_kit_custom_byline_section',
        'priority'          => 22,
        'default'           => '#FBFBFB',
        'transport'         => 'postMessage',
        'sanitize_callback' => 'sanitize_hex_color',
    ],
    [
        'type'              => 'color',
        'settings'          => 'author_kit_byline_text_color_default',
        'label'             => esc_html__('Text Color', 'author-kit'),
        'section'           => 'author_kit_custom_byline_section',
        'priority'          => 22,
        'default'           => '#333',
        'transport'         => 'postMessage',
        'sanitize_callback' => 'sanitize_hex_color',
    ],
    [
        'type'              => 'color',
        'settings'          => 'author_kit_byline_separator_color_default',
        'label'             => esc_html__('Separator Color', 'author-kit'),
        'section'           => 'author_kit_custom_byline_section',
        'priority'          => 25,
        'default'           => '#333',
        'transport'         => 'postMessage',
        'sanitize_callback' => 'sanitize_hex_color',
    ],
    
    // 3. Slider
    [
        'type'        => 'slider',
        'settings'    => 'author_kit_byline_font_size_default',
        'label'       => esc_html__('Font Size (px)', 'author-kit'),
        'description' => esc_html__('Set the font size of the byline for different devices.', 'author-kit'),
        'section'     => 'author_kit_custom_byline_section',
        'priority'    => 30,
        'responsive'  => true,
        'default'     => [
            'desktop' => 14,
            'mobile'  => 12,
        ],
        'choices'     => [
            'min'  => 1,
            'max'  => 50,
            'step' => 1,
        ],
        'transport'   => 'postMessage',
        'output'      => [
            [
                'element'     => [
                    '.author-kit-main-author-label',
                    '.author-kit-contributions-label',
                    '.author-kit-last-updated',
                    '.author-kit-published-date',
                    '.author-kit-byline-container a',
                    '.author-kit-category-label',
                    '.author-kit-separator',
                ],
                'property'    => 'font-size',
                'units'       => 'px',
                'media_query' => [
                    'desktop' => '@media (min-width: 900px)',
                    'mobile'  => '@media (max-width: 899px)',
                ],
            ],
        ],
    ],
    
    
    [
        'type'     => 'radio-button',
        'settings' => 'author_kit_byline_alignment_default',
        'label'    => esc_html__( 'Justify Content', 'author-kit' ),
        'section'  => 'author_kit_custom_byline_section',
        'default'  => 'left',
        'transport'   => 'postMessage',
        'priority' => 10,
        'choices'  => [
            'left'   => esc_html__( 'Left', 'author-kit' ),
            'center' => esc_html__( 'Center', 'author-kit' ),
            'right'  => esc_html__( 'Right', 'author-kit' ),
        ],
        
    ],
    // 4. Text Settings
    [
        'type'     => 'text',
        'settings' => 'author_kit_byline_written_by_label_default',
        'label'    => esc_html__('Label: Published By', 'author-kit'),
        'section'  => 'author_kit_custom_byline_section',
        'priority' => 40,
        'default'  => esc_html__('Published By', 'author-kit'),
        'transport' => 'postMessage',
    ],
    [
        'type'     => 'text',
        'settings' => 'author_kit_byline_contribution_from_label_default',
        'label'    => esc_html__('Label: Co Authors', 'author-kit'),
        'section'  => 'author_kit_custom_byline_section',
        'priority' => 42,
        'default'  => esc_html__('Co Authors', 'author-kit'),
        'transport' => 'postMessage',
    
    ],
    [
        'type'     => 'text',
        'settings' => 'author_kit_byline_separator_label_default',
        'label'    => esc_html__('Separator', 'author-kit'),
        'section'  => 'author_kit_custom_byline_section',
        'priority' => 42,
        'default'  => esc_html__('|', 'author-kit'),
        'transport' => 'postMessage',
    ],
    
    // 5. Switches (updated to use integer values: 1 for enabled, 0 for disabled)
    [
        'type'        => 'switch',
        'settings'    => 'author_kit_byline_hide_avatar_default',
        'label'       => esc_html__('Hide Avatar', 'author-kit'),
        'description' => esc_html__('Hide or Display the avatar on bylines', 'author-kit'),
        'section'     => 'author_kit_custom_byline_section',
        'priority'    => 43,
        'default'     => 0,
        'choices'     => [
            1 => esc_html__('Enabled', 'author-kit'),
            0 => esc_html__('Disabled', 'author-kit'),
        ],
    ],
    [
        'type'        => 'switch',
        'settings'    => 'author_kit_byline_hide_post_date_default',
        'label'       => esc_html__('Hide Post Date', 'author-kit'),
        'description' => esc_html__('Hide or Display the post date on bylines', 'author-kit'),
        'section'     => 'author_kit_custom_byline_section',
        'priority'    => 43,
        'default'     => 0,
        'choices'     => [
            1 => esc_html__('Enabled', 'author-kit'),
            0 => esc_html__('Disabled', 'author-kit'),
        ],
    ],
    [
        'type'        => 'switch',
        'settings'    => 'author_kit_byline_hide_last_updated_date_default',
        'label'       => esc_html__('Hide Last Updated Date', 'author-kit'),
        'description' => esc_html__('Hide or Display the last updated date on bylines', 'author-kit'),
        'section'     => 'author_kit_custom_byline_section',
        'priority'    => 44,
        'default'     => 1,
        'choices'     => [
            1 => esc_html__('Enabled', 'author-kit'),
            0 => esc_html__('Disabled', 'author-kit'),
        ],
    ],
    [
        'type'        => 'switch',
        'settings'    => 'author_kit_byline_hide_categories_default',
        'label'       => esc_html__('Hide Categories', 'author-kit'),
        'description' => esc_html__('Hide or Display the Categories on bylines', 'author-kit'),
        'section'     => 'author_kit_custom_byline_section',
        'priority'    => 45,
        'default'     => 1,
        'choices'     => [
            1 => esc_html__('Enabled', 'author-kit'),
            0 => esc_html__('Disabled', 'author-kit'),
        ],
    ],
    [
        'type'        => 'switch',
        'settings'    => 'author_kit_byline_hide_comments_default',
        'label'       => esc_html__('Hide Comments', 'author-kit'),
        'description' => esc_html__('Hide or Display the comments count on bylines', 'author-kit'),
        'section'     => 'author_kit_custom_byline_section',
        'priority'    => 46,
        'transport'   => 'refresh',
        'default'     => 1,
        'choices'     => [
            1 => esc_html__('Enabled', 'author-kit'),
            0 => esc_html__('Disabled', 'author-kit'),
        ],
    ],
    

    ];
     $template_controls['template-advo'] = [
        // 1. Dimensions
        [
         'type'        => 'dimensions',
         'settings'    => 'author_kit_byline_dimensions_advo',
         'label'       => esc_html__('Byline Box Dimensions', 'author-kit'),
         'description' => esc_html__('Set the width and height of the byline box.', 'author-kit'),
         'section'     => 'author_kit_custom_byline_section',
         'priority'    => 10,
         'default'     => [
             'width'  => '100%',
             'height' => 'auto',
         ],
         'transport'   => 'postMessage',
     ],
     
     // 2. Background Colors
     [
         'type'              => 'color',
         'settings'          => 'author_kit_byline_bg_color_advo',
         'label'             => esc_html__('Background Color', 'author-kit'),
         'section'           => 'author_kit_custom_byline_section',
         'priority'          => 20,
         'default'           => 'inherit',
         'transport'         => 'postMessage',
         'sanitize_callback' => 'sanitize_hex_color',
     ],
     [
         'type'              => 'color',
         'settings'          => 'author_kit_byline_text_color_advo',
         'label'             => esc_html__('Text Color', 'author-kit'),
         'section'           => 'author_kit_custom_byline_section',
         'priority'          => 22,
         'default'           => '#333',
         'transport'         => 'postMessage',
         'sanitize_callback' => 'sanitize_hex_color',
     ],
   
     [
         'type'              => 'color',
         'settings'          => 'author_kit_byline_separator_color_advo',
         'label'             => esc_html__('Separator Color', 'author-kit'),
         'section'           => 'author_kit_custom_byline_section',
         'priority'          => 25,
         'default'           => '#333',
         'transport'         => 'postMessage',
         'sanitize_callback' => 'sanitize_hex_color',
     ],
     
     // 3. Slider
     [
        'type'        => 'slider',
        'settings'    => 'author_kit_byline_font_size_advo',
        'label'       => esc_html__('Font Size (px)', 'author-kit'),
        'description' => esc_html__('Set the font size of the byline for different devices.', 'author-kit'),
        'section'     => 'author_kit_custom_byline_section',
        'priority'    => 30,
        'responsive'  => true,
        'default'     => [
            'desktop' => 14,
            'mobile'  => 12,
        ],
        'choices'     => [
            'min'  => 1,
            'max'  => 50,
            'step' => 1,
        ],
        'transport'   => 'postMessage',
        'output'      => [
            [
                'element'     => [
                    '.author-kit-main-author-label',
                    '.author-kit-contributions-label',
                    '.author-kit-last-updated',
                    '.author-kit-published-date',
                    '.author-kit-byline-container a',
                    '.author-kit-category-label',
                    '.author-kit-separator',
                ],
                'property'    => 'font-size',
                'units'       => 'px',
                'media_query' => [
                    'desktop' => '@media (min-width: 900px)',
                    'mobile'  => '@media (max-width: 899px)',
                ],
            ],
        ],
    ],
    
     [
         'type'     => 'radio-button',
         'settings' => 'author_kit_byline_alignment_advo',
         'label'    => esc_html__( 'Justify Content', 'author-kit' ),
         'section'  => 'author_kit_custom_byline_section',
         'default'  => 'left',
         'transport'   => 'postMessage',
         'priority' => 10,
         'choices'  => [
             'left'   => esc_html__( 'Left', 'author-kit' ),
             'center' => esc_html__( 'Center', 'author-kit' ),
             'right'  => esc_html__( 'Right', 'author-kit' ),
         ],
         
     ],
     // 4. Text Settings
     [
         'type'     => 'text',
         'settings' => 'author_kit_byline_written_by_label_advo',
         'label'    => esc_html__('Label: Article By', 'author-kit'),
         'section'  => 'author_kit_custom_byline_section',
         'priority' => 40,
         'default'  => esc_html__('Written By', 'author-kit'),
         'transport' => 'postMessage',
     ],
     [
         'type'     => 'text',
         'settings' => 'author_kit_byline_contribution_from_label_advo',
         'label'    => esc_html__('Label: Other Writers', 'author-kit'),
         'section'  => 'author_kit_custom_byline_section',
         'priority' => 42,
         'default'  => esc_html__('Other Writers', 'author-kit'),
         'transport' => 'postMessage',
     
     ],
     [
         'type'     => 'text',
         'settings' => 'author_kit_byline_separator_label_advo',
         'label'    => esc_html__('Separator', 'author-kit'),
         'section'  => 'author_kit_custom_byline_section',
         'priority' => 42,
         'default'  => esc_html__('|', 'author-kit'),
         'transport' => 'postMessage',
     ],
     
     // 5. Switches (updated to use integer values: 1 for enabled, 0 for disabled)
     [
         'type'        => 'switch',
         'settings'    => 'author_kit_byline_hide_avatar_advo',
         'label'       => esc_html__('Hide Avatar', 'author-kit'),
         'description' => esc_html__('Hide or Display the avatar on bylines', 'author-kit'),
         'section'     => 'author_kit_custom_byline_section',
         'priority'    => 43,
         'default'     => 0,
         'choices'     => [
             1 => esc_html__('Enabled', 'author-kit'),
             0 => esc_html__('Disabled', 'author-kit'),
         ],
     ],
     [
         'type'        => 'switch',
         'settings'    => 'author_kit_byline_hide_post_date_advo',
         'label'       => esc_html__('Hide Post Date', 'author-kit'),
         'description' => esc_html__('Hide or Display the post date on bylines', 'author-kit'),
         'section'     => 'author_kit_custom_byline_section',
         'priority'    => 43,
         'default'     => 0,
         'choices'     => [
             1 => esc_html__('Enabled', 'author-kit'),
             0 => esc_html__('Disabled', 'author-kit'),
         ],
     ],
     [
         'type'        => 'switch',
         'settings'    => 'author_kit_byline_hide_last_updated_date_advo',
         'label'       => esc_html__('Hide Last Updated Date', 'author-kit'),
         'description' => esc_html__('Hide or Display the last updated date on bylines', 'author-kit'),
         'section'     => 'author_kit_custom_byline_section',
         'priority'    => 44,
         'default'     => 0,
         'choices'     => [
             1 => esc_html__('Enabled', 'author-kit'),
             0 => esc_html__('Disabled', 'author-kit'),
         ],
     ],
     [
         'type'        => 'switch',
         'settings'    => 'author_kit_byline_hide_categories_advo',
         'label'       => esc_html__('Hide Categories', 'author-kit'),
         'description' => esc_html__('Hide or Display the Categories on bylines', 'author-kit'),
         'section'     => 'author_kit_custom_byline_section',
         'priority'    => 45,
         'default'     => 1,
         'choices'     => [
             1 => esc_html__('Enabled', 'author-kit'),
             0 => esc_html__('Disabled', 'author-kit'),
         ],
     ],
     [
         'type'        => 'switch',
         'settings'    => 'author_kit_byline_hide_comments_advo',
         'label'       => esc_html__('Hide Comments', 'author-kit'),
         'description' => esc_html__('Hide or Display the comments count on bylines', 'author-kit'),
         'section'     => 'author_kit_custom_byline_section',
         'priority'    => 46,
         'transport'   => 'refresh',
         'default'     => 1,
         'choices'     => [
             1 => esc_html__('Enabled', 'author-kit'),
             0 => esc_html__('Disabled', 'author-kit'),
         ],
     ],
     
    ];
    $template_controls['template-elantra'] =  [
        // 1. Dimensions
        [
         'type'        => 'dimensions',
         'settings'    => 'author_kit_byline_dimensions_elantra',
         'label'       => esc_html__('Byline Box Dimensions', 'author-kit'),
         'description' => esc_html__('Set the width and height of the byline box.', 'author-kit'),
         'section'     => 'author_kit_custom_byline_section',
         'priority'    => 10,
         'default'     => [
             'width'  => '100%',
             'height' => 'auto',
         ],
         'transport'   => 'postMessage',
     ],
     
     // 2. Background Colors
     [
         'type'              => 'color',
         'settings'          => 'author_kit_byline_bg_color_elantra',
         'label'             => esc_html__('Background Color', 'author-kit'),
         'section'           => 'author_kit_custom_byline_section',
         'priority'          => 20,
         'default'           => 'inherit',
         'transport'         => 'postMessage',
         'sanitize_callback' => 'sanitize_hex_color',
     ],
     [
         'type'              => 'color',
         'settings'          => 'author_kit_byline_text_color_elantra',
         'label'             => esc_html__('Text Color', 'author-kit'),
         'section'           => 'author_kit_custom_byline_section',
         'priority'          => 22,
         'default'           => '#333',
         'transport'         => 'postMessage',
         'sanitize_callback' => 'sanitize_hex_color',
     ],

     [
         'type'              => 'color',
         'settings'          => 'author_kit_byline_separator_color_elantra',
         'label'             => esc_html__('Separator Color', 'author-kit'),
         'section'           => 'author_kit_custom_byline_section',
         'priority'          => 25,
         'default'           => '#333',
         'transport'         => 'postMessage',
         'sanitize_callback' => 'sanitize_hex_color',
     ],
     
     // 3. Slider
     [
        'type'        => 'slider',
        'settings'    => 'author_kit_byline_font_size_elantra',
        'label'       => esc_html__('Font Size (px)', 'author-kit'),
        'description' => esc_html__('Set the font size of the byline for different devices.', 'author-kit'),
        'section'     => 'author_kit_custom_byline_section',
        'priority'    => 30,
        'responsive'  => true,
        'default'     => [
            'desktop' => 14,
            'mobile'  => 12,
        ],
        'choices'     => [
            'min'  => 1,
            'max'  => 50,
            'step' => 1,
        ],
        'transport'   => 'postMessage',
        'output'      => [
            [
                'element'     => [
                    '.author-kit-main-author-label',
                    '.author-kit-contributions-label',
                    '.author-kit-last-updated',
                    '.author-kit-published-date',
                    '.author-kit-byline-container a',
                    '.author-kit-category-label',
                    '.author-kit-separator',
                ],
                'property'    => 'font-size',
                'units'       => 'px',
                'media_query' => [
                    'desktop' => '@media (min-width: 900px)',
                    'mobile'  => '@media (max-width: 899px)',
                ],
            ],
        ],
    ],
    
     
     [
         'type'     => 'radio-button',
         'settings' => 'author_kit_byline_alignment_elantra',
         'label'    => esc_html__( 'Justify Content', 'author-kit' ),
         'section'  => 'author_kit_custom_byline_section',
         'default'  => 'left',
         'transport'   => 'postMessage',
         'priority' => 10,
         'choices'  => [
             'left'   => esc_html__( 'Left', 'author-kit' ),
             'center' => esc_html__( 'Center', 'author-kit' ),
             'right'  => esc_html__( 'Right', 'author-kit' ),
         ],
         
     ],
     // 4. Text Settings
     [
         'type'     => 'text',
         'settings' => 'author_kit_byline_written_by_label_elantra',
         'label'    => esc_html__('Label: Written By', 'author-kit'),
         'section'  => 'author_kit_custom_byline_section',
         'priority' => 40,
         'default'  => esc_html__('Written By', 'author-kit'),
         'transport' => 'postMessage',
     ],
     [
         'type'     => 'text',
         'settings' => 'author_kit_byline_contribution_from_label_elantra',
         'label'    => esc_html__('Label: Contribution From', 'author-kit'),
         'section'  => 'author_kit_custom_byline_section',
         'priority' => 42,
         'default'  => esc_html__('Contribution From', 'author-kit'),
         'transport' => 'postMessage',
     
     ],
     [
         'type'     => 'text',
         'settings' => 'author_kit_byline_separator_label_elantra',
         'label'    => esc_html__('Separator', 'author-kit'),
         'section'  => 'author_kit_custom_byline_section',
         'priority' => 42,
         'default'  => esc_html__('|', 'author-kit'),
         'transport' => 'postMessage',
     ],
     
     // 5. Switches (updated to use integer values: 1 for enabled, 0 for disabled)
     [
         'type'        => 'switch',
         'settings'    => 'author_kit_byline_hide_avatar_elantra',
         'label'       => esc_html__('Hide Avatar', 'author-kit'),
         'description' => esc_html__('Hide or Display the avatar on bylines', 'author-kit'),
         'section'     => 'author_kit_custom_byline_section',
         'priority'    => 43,
         'default'     => 0,
         'choices'     => [
             1 => esc_html__('Enabled', 'author-kit'),
             0 => esc_html__('Disabled', 'author-kit'),
         ],
     ],
     [
         'type'        => 'switch',
         'settings'    => 'author_kit_byline_hide_post_date_elantra',
         'label'       => esc_html__('Hide Post Date', 'author-kit'),
         'description' => esc_html__('Hide or Display the post date on bylines', 'author-kit'),
         'section'     => 'author_kit_custom_byline_section',
         'priority'    => 43,
         'default'     => 0,
         'choices'     => [
             1 => esc_html__('Enabled', 'author-kit'),
             0 => esc_html__('Disabled', 'author-kit'),
         ],
     ],
     [
         'type'        => 'switch',
         'settings'    => 'author_kit_byline_hide_last_updated_date_elantra',
         'label'       => esc_html__('Hide Last Updated Date', 'author-kit'),
         'description' => esc_html__('Hide or Display the last updated date on bylines', 'author-kit'),
         'section'     => 'author_kit_custom_byline_section',
         'priority'    => 44,
         'default'     => 0,
         'choices'     => [
             1 => esc_html__('Enabled', 'author-kit'),
             0 => esc_html__('Disabled', 'author-kit'),
         ],
     ],
     [
         'type'        => 'switch',
         'settings'    => 'author_kit_byline_hide_categories_elantra',
         'label'       => esc_html__('Hide Categories', 'author-kit'),
         'description' => esc_html__('Hide or Display the Categories on bylines', 'author-kit'),
         'section'     => 'author_kit_custom_byline_section',
         'priority'    => 45,
         'default'     => 0,
         'choices'     => [
             1 => esc_html__('Enabled', 'author-kit'),
             0 => esc_html__('Disabled', 'author-kit'),
         ],
     ],
     [
         'type'        => 'switch',
         'settings'    => 'author_kit_byline_hide_comments_elantra',
         'label'       => esc_html__('Hide Comments', 'author-kit'),
         'description' => esc_html__('Hide or Display the comments count on bylines', 'author-kit'),
         'section'     => 'author_kit_custom_byline_section',
         'priority'    => 46,
         'transport'   => 'refresh',
         'default'     => 0,
         'choices'     => [
             1 => esc_html__('Enabled', 'author-kit'),
             0 => esc_html__('Disabled', 'author-kit'),
         ],
     ],
     
     
 

        ];

    // Apply controls for the active template
    if (!empty($template_controls[$active_template])) {
        foreach ($template_controls[$active_template] as $control) {
            $control_type = $control['type'] ?? null;

            // Ensure the control type exists and the corresponding class is defined
            if ($control_type && !empty($control_classes[$control_type])) {
                $control_class = $control_classes[$control_type];

                if (class_exists($control_class)) {
                    new $control_class($control);
                } else {
                       continue ;           
                     }
            }
        }
    }
}
add_action('kirki_config', 'author_kit_custom_byline_customizer');
