<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

class IDSC_Elementor_TOC extends \Elementor\Widget_Base {

    public function get_name() {
        return 'idsc_toc';
    }

    public function get_title() {
        return __( 'Table of Contents', 'idsc' );
    }

    public function get_icon() {
        return 'eicon-table-of-contents';
    }

    public function get_categories() {
        return array( 'idsc' );
    }

    public function get_keywords() {
        return array( 'toc', 'table of contents', 'headings', 'navigation' );
    }

    protected function register_controls() {
        $this->start_controls_section( 'section_content', array(
            'label' => __( 'Table of Contents', 'idsc' ),
        ) );

        $this->add_control( 'levels', array(
            'label'       => __( 'Heading Levels', 'idsc' ),
            'type'        => \Elementor\Controls_Manager::TEXT,
            'default'     => 'h2,h3',
            'description' => __( 'Comma-separated heading levels (e.g. h2,h3,h4).', 'idsc' ),
        ) );

        $this->add_control( 'title', array(
            'label'   => __( 'Title', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::TEXT,
            'default' => 'On This Page',
        ) );

        $this->add_control( 'style', array(
            'label'   => __( 'Style', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::SELECT,
            'default' => 'numbered',
            'options' => array(
                'numbered' => __( 'Numbered', 'idsc' ),
                'bulleted' => __( 'Bulleted', 'idsc' ),
            ),
        ) );

        $this->add_control( 'collapsible', array(
            'label'        => __( 'Collapsible', 'idsc' ),
            'type'         => \Elementor\Controls_Manager::SWITCHER,
            'label_on'     => __( 'Yes', 'idsc' ),
            'label_off'    => __( 'No', 'idsc' ),
            'return_value' => 'yes',
            'default'      => '',
        ) );

        $this->end_controls_section();
    }

    protected function render() {
        $s = $this->get_settings_for_display();

        $atts  = ' levels="' . esc_attr( $s['levels'] ) . '"';
        $atts .= ! empty( $s['title'] ) ? ' title="' . esc_attr( $s['title'] ) . '"' : '';
        $atts .= ' style="' . esc_attr( $s['style'] ) . '"';
        $atts .= $s['collapsible'] === 'yes' ? ' collapsible="yes"' : '';

        echo do_shortcode( '[idsc_toc' . $atts . ']' );
    }
}
