<?php
/**
 * IDVE (Insight Dezign Visual Editor) Integration
 *
 * Registers the Business Hours block for the IDVE editor.
 *
 * @package ID_Business_Listing
 * @license GPL-2.0-or-later
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

class BLM_IDVE {

    public function __construct() {
        add_action( 'idve_register_blocks', array( $this, 'register_blocks' ) );
    }

    public function register_blocks( $blocks ) {

        $blocks->register( 'business_hours', array(
            'label'    => 'Business Hours',
            'icon'     => 'clock',
            'category' => 'shortcodes',
            'fields'   => array(
                'show_closed'     => array( 'type' => 'select', 'label' => 'Show Closed Days', 'options' => array( 'yes', 'no' ), 'default' => 'yes' ),
                'highlight_today' => array( 'type' => 'select', 'label' => 'Highlight Today', 'options' => array( 'yes', 'no' ), 'default' => 'yes' ),
                'time_format'     => array( 'type' => 'select', 'label' => 'Time Format', 'options' => array( '12', '24' ), 'default' => '12' ),
                'class'           => array( 'type' => 'text', 'label' => 'CSS Class', 'default' => '' ),
            ),
            'render' => array( $this, 'render_business_hours' ),
        ) );
    }

    public function render_business_hours( $data, $settings ) {
        $defaults = array(
            'show_closed'     => 'yes',
            'highlight_today' => 'yes',
            'time_format'     => '12',
            'class'           => '',
        );

        $atts = '';

        foreach ( $defaults as $key => $default ) {
            $value = isset( $data[ $key ] ) ? $data[ $key ] : $default;
            if ( (string) $value !== (string) $default ) {
                $atts .= ' ' . $key . '="' . esc_attr( $value ) . '"';
            }
        }

        return '[business_hours' . $atts . ']';
    }
}
