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

add_shortcode( 'idsc_counter', 'idsc_counter_shortcode' );

function idsc_counter_shortcode( $atts ) {
    IDSC_Assets::flag_css();
    IDSC_Assets::flag_js();

    $atts = shortcode_atts( array(
        'number'   => '100',
        'prefix'   => '',
        'suffix'   => '',
        'label'    => '',
        'duration' => '2000',
    ), $atts, 'idsc_counter' );

    $number   = intval( $atts['number'] );
    $duration = max( 100, intval( $atts['duration'] ) );

    $html  = '<div class="idsc-counter" data-idsc-counter data-target="' . esc_attr( $number ) . '" data-duration="' . esc_attr( $duration ) . '">';
    $html .= '<div class="idsc-counter__number">';

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

    $html .= '<span class="idsc-counter__value">0</span>';

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

    $html .= '</div>';

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

    $html .= '</div>';

    return $html;
}
