<?php

namespace WPIS;

class VueEstateListingShortcode {

    private static $shortcode = 'wpis-estate-listing';

    static public function init() {

        $class = new self();

        add_shortcode( self::$shortcode, [$class, 'generateShortcode'] );
    }

    public function generateShortcode($atts = false, $content = null) {

        $options = [];
        $supportedOptions = ['disabled-filters', 'filters', 'hide-sold-estates'];

        if( $atts ){
            foreach($atts as $property => $value){
                if( in_array( $property, $supportedOptions ) ){
                    $optionName = Tools::formatDashesToCamelCase($property);
                    $options[$optionName] = $value;
                    if( strpos($value, ',') ){
                        $values = explode(',', $value);
                        foreach( $values as $i => $value){
                            $values[$i] = trim($value);
                        }
                        $options[$optionName] = $values;
                    }
                    elseif( in_array($optionName, ['disabledFilters', 'filters']) ){
                        $options[$optionName] = $value !== '' ? [$value] : [];
                    }
                }
            }
        }

        $options = json_encode($options);

        return '<div id="wpis-estate-listing" options=\'' . $options . '\'></div>';
    }
}