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

add_shortcode( 'idsc_alert', 'idsc_alert_shortcode' );

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

    $atts = shortcode_atts( array(
        'type'        => 'info',
        'title'       => '',
        'dismissible' => 'no',
        'icon'        => 'yes',
    ), $atts, 'idsc_alert' );

    $type  = sanitize_html_class( $atts['type'] );
    $class = 'idsc-alert idsc-alert--' . $type;

    if ( $atts['dismissible'] === 'yes' ) {
        $class .= ' idsc-alert--dismissible';
        IDSC_Assets::flag_js();
    }

    $icons = array(
        'info'    => '<svg class="idsc-alert__icon" viewBox="0 0 20 20" fill="currentColor"><circle cx="10" cy="10" r="9" fill="none" stroke="currentColor" stroke-width="1.5"/><path d="M10 9v5M10 6.5v.01"/></svg>',
        'success' => '<svg class="idsc-alert__icon" viewBox="0 0 20 20" fill="currentColor"><circle cx="10" cy="10" r="9" fill="none" stroke="currentColor" stroke-width="1.5"/><path d="M6 10l3 3 5-5" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/></svg>',
        'warning' => '<svg class="idsc-alert__icon" viewBox="0 0 20 20" fill="currentColor"><path d="M10 2L1 18h18L10 2z" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linejoin="round"/><path d="M10 8v4M10 14.5v.01"/></svg>',
        'error'   => '<svg class="idsc-alert__icon" viewBox="0 0 20 20" fill="currentColor"><circle cx="10" cy="10" r="9" fill="none" stroke="currentColor" stroke-width="1.5"/><path d="M7 7l6 6M13 7l-6 6" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/></svg>',
    );

    $html = '<div class="' . esc_attr( $class ) . '" role="alert">';

    if ( $atts['icon'] === 'yes' && isset( $icons[ $type ] ) ) {
        $html .= $icons[ $type ];
    }

    $html .= '<div class="idsc-alert__body">';

    if ( ! empty( $atts['title'] ) ) {
        $html .= '<strong class="idsc-alert__title">' . esc_html( $atts['title'] ) . '</strong>';
    }

    $html .= '<div class="idsc-alert__message">' . wp_kses_post( $content ) . '</div>';
    $html .= '</div>';

    if ( $atts['dismissible'] === 'yes' ) {
        $html .= '<button type="button" class="idsc-alert__close" aria-label="Dismiss">&times;</button>';
    }

    $html .= '</div>';

    return $html;
}
