<?php
/**
 * The template part for displaying all the Elements.
 * 
 * @package Newsxo
 * 
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {	exit; }


function newsxo_output_repeater( $setting_id ) {

    $data = newsxo_get_option( $setting_id );

    if ( empty( $data ) ) {
        return;
    }

    // Decode JSON
    $items = json_decode( $data, true );

    if ( ! is_array( $items ) ) {
        return;
    }

    foreach ( $items as $item ) {

        $type     = $item['select']   ?? '';
        $columnVal   = $item['column']   ?? '';
        $positionVal = $item['position'] ?? '';

        $column = !empty( $columnVal ) ? 'col-width-' . $columnVal : '';
        $columnWidth = !empty( $columnVal ) ? 'style="--col-width:' . $columnVal . '%"' : '';
        $position = !empty( $positionVal ) ?  'text-center-xs text-lg-' . $positionVal : '';


        $classes = array_filter( [ $column, $position ] );

        if ( ! empty( $classes ) ) {
            echo '<div class="' . esc_attr( implode( ' ', $classes ) ) . '" '. $columnWidth . '>';
        }

        switch ( $type ) {

            case 'date-time':
                get_template_part( 'template-parts/elements/header/el', 'date-time' );
                break;

            case 'head-social-icon':
                get_template_part( 'template-parts/elements/header/el', 'social-icon', array( 'location' => 'header' ) );
                break;

            case 'logo-title':
                get_template_part( 'template-parts/elements/header/el','logo-title' );
                break;

            case 'pri-menus':
                get_template_part( 'template-parts/elements/header/el','primary-menus' );
                break;

            case 'right-nav':
                get_template_part( 'template-parts/elements/header/el','right-nav' );
                break;

            case 'ticker':
                get_template_part( 'template-parts/elements/header/el','news-ticker' );
                break;

            case 'wid-area':
                get_template_part( 'template-parts/elements/footer/el','wid-area' );
                break;

            case 'foot-logo-title':
                get_template_part( 'template-parts/elements/footer/el','logo-title' );
                break;

            case 'foot-social-icon':
                get_template_part( 'template-parts/elements/header/el','social-icon', array( 'location' => 'footer' ));
                break;

            case 'foot-menus':
                get_template_part( 'template-parts/elements/footer/el','footer-menus' );
                break;

            case 'copyright':
                get_template_part( 'template-parts/elements/footer/el','copyright' );
                break;

            default:
                /**
                 * Allow child theme / plugin output
                 */
                do_action( 'newsxo_render_repeater_item', $type, $item );
                break;
        }
        if ( !empty( $position ) || !empty( $column)) {
            echo '</div>';
        }
    }
}