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

add_shortcode( 'idsc_icon_list', 'idsc_icon_list_shortcode' );
add_shortcode( 'idsc_icon_list_item', 'idsc_icon_list_item_shortcode' );

function idsc_icon_list_shortcode( $atts, $content = '' ) {
    IDSC_Assets::flag_css();

    $atts = shortcode_atts( array(
        'icon'       => 'check',
        'icon_color' => '#22c55e',
        'icon_size'  => 'medium',
    ), $atts, 'idsc_icon_list' );

    // Store context for child shortcodes
    $GLOBALS['idsc_icon_list_ctx'] = $atts;

    $size_class = sanitize_html_class( 'idsc-icon-list--' . $atts['icon_size'] );
    $inner      = do_shortcode( $content );

    unset( $GLOBALS['idsc_icon_list_ctx'] );

    return '<ul class="idsc-icon-list ' . $size_class . '" style="--idsc-icon-color:' . esc_attr( $atts['icon_color'] ) . '">' . $inner . '</ul>';
}

function idsc_icon_list_item_shortcode( $atts, $content = '' ) {
    $ctx = isset( $GLOBALS['idsc_icon_list_ctx'] ) ? $GLOBALS['idsc_icon_list_ctx'] : array();

    $atts = shortcode_atts( array(
        'icon' => isset( $ctx['icon'] ) ? $ctx['icon'] : 'check',
    ), $atts, 'idsc_icon_list_item' );

    $icon_svg = idsc_get_icon_svg( $atts['icon'] );

    return '<li class="idsc-icon-list__item">' . $icon_svg . '<span class="idsc-icon-list__text">' . wp_kses_post( $content ) . '</span></li>';
}

function idsc_get_icon_svg( $name ) {
    $icons = array(
        'check'       => '<svg viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="4,10 8,14 16,6"/></svg>',
        'x'           => '<svg viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><line x1="5" y1="5" x2="15" y2="15"/><line x1="15" y1="5" x2="5" y2="15"/></svg>',
        'arrow-right' => '<svg viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="3" y1="10" x2="17" y2="10"/><polyline points="12,5 17,10 12,15"/></svg>',
        'arrow-left'  => '<svg viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="17" y1="10" x2="3" y2="10"/><polyline points="8,5 3,10 8,15"/></svg>',
        'star'        => '<svg viewBox="0 0 20 20" fill="currentColor"><polygon points="10,1.5 12.47,7.27 18.76,7.85 13.97,12.05 15.35,18.2 10,15.08 4.65,18.2 6.03,12.05 1.24,7.85 7.53,7.27"/></svg>',
        'heart'       => '<svg viewBox="0 0 20 20" fill="currentColor"><path d="M10 17.5s-7-4.5-7-9A3.5 3.5 0 0 1 10 5.5 3.5 3.5 0 0 1 17 8.5c0 4.5-7 9-7 9z"/></svg>',
        'info'        => '<svg viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="1.5"><circle cx="10" cy="10" r="8"/><line x1="10" y1="9" x2="10" y2="14"/><circle cx="10" cy="6.5" r="0.5" fill="currentColor"/></svg>',
        'warning'     => '<svg viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linejoin="round"><path d="M10 2L1 18h18L10 2z"/><line x1="10" y1="8" x2="10" y2="12"/><circle cx="10" cy="14.5" r="0.5" fill="currentColor"/></svg>',
        'plus'        => '<svg viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><line x1="10" y1="4" x2="10" y2="16"/><line x1="4" y1="10" x2="16" y2="10"/></svg>',
        'minus'       => '<svg viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><line x1="4" y1="10" x2="16" y2="10"/></svg>',
        'phone'       => '<svg viewBox="0 0 20 20" fill="currentColor"><path d="M4 3a1 1 0 0 1 1-1h2.28a1 1 0 0 1 .95.68l1 3a1 1 0 0 1-.27 1.05L7.4 8.3a10 10 0 0 0 4.3 4.3l1.57-1.56a1 1 0 0 1 1.05-.27l3 1a1 1 0 0 1 .68.95V15a1 1 0 0 1-1 1A14 14 0 0 1 4 3z"/></svg>',
        'email'       => '<svg viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><rect x="2" y="4" width="16" height="12" rx="1"/><polyline points="2,4 10,11 18,4"/></svg>',
        'link'        => '<svg viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"><path d="M8.5 11.5a4 4 0 0 0 5.66 0l2-2a4 4 0 0 0-5.66-5.66l-1 1"/><path d="M11.5 8.5a4 4 0 0 0-5.66 0l-2 2a4 4 0 0 0 5.66 5.66l1-1"/></svg>',
        'clock'       => '<svg viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"><circle cx="10" cy="10" r="8"/><polyline points="10,5 10,10 13.5,12"/></svg>',
        'location'    => '<svg viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="1.5"><path d="M10 18s-6-5.3-6-9.5a6 6 0 1 1 12 0C16 12.7 10 18 10 18z"/><circle cx="10" cy="8.5" r="2"/></svg>',
        'user'        => '<svg viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><circle cx="10" cy="7" r="3.5"/><path d="M3 18a7 7 0 0 1 14 0"/></svg>',
    );

    $svg = isset( $icons[ $name ] ) ? $icons[ $name ] : $icons['check'];
    return '<span class="idsc-icon-list__icon">' . $svg . '</span>';
}
