<?php
/**
 * Event Location Block
 *
 * @package NemtlyBooking\Blocks
 */

namespace NemtlyBooking\Blocks;

/**
 * Event Location Block class
 */
class Event_Location extends Abstract_Block {

    /**
     * Block Name
     *
     * @var string
     */
    protected $block_name = 'event-location';

    /**
     * Render the block
     *
     * @param array    $attributes Block attributes.
     * @param string   $content    Block content.
     * @param \WP_Block $block      Block instance.
     * @return string
     */
    public function render( $attributes, $content, $block ) {
        $post_id = isset( $block->context['postId'] ) ? $block->context['postId'] : 0;

        // Verify post type
        if ( empty( $post_id ) || get_post_type( $post_id ) !== 'nemtly_event' ) {
            return '';
        }

        $location = get_post_meta( $post_id, 'nemtly_location', true );

        if ( empty( $location ) ) {
            return '';
        }

        /**
         * Filter the CSS classes applied to the Event Location block wrapper.
         *
         * @since PBOOKING_SINCE
         *
         * @param string[] $classes    Wrapper CSS classes.
         * @param array    $attributes Block attributes.
         * @param int      $post_id    Event post ID.
         */
        $classes = apply_filters( 'nemtly_booking_block_event_location_classes', [ 'nemtly-event-location' ], $attributes, $post_id );

        $wrapper_attributes = get_block_wrapper_attributes(
            [
                'class' => implode( ' ', $classes ),
            ]
        );

        $icon = '';
        if ( isset( $attributes['showIcon'] ) && $attributes['showIcon'] ) {
            $icon = '<svg class="nb-w-5 nb-h-5 nb-mr-2" fill="currentColor" viewBox="0 0 20 20"><path fill-rule="evenodd" d="M5.05 4.05a7 7 0 119.9 9.9L10 18.9l-4.95-4.95a7 7 0 010-9.9zM10 11a2 2 0 100-4 2 2 0 000 4z" clip-rule="evenodd" /></svg>';
        }

        /**
         * Filter the resolved render data for the Event Location block.
         *
         * @since PBOOKING_SINCE
         *
         * @param array $data       {
         *     @type string $location Location text.
         *     @type string $icon     Icon markup (empty if not shown).
         * }
         * @param array $attributes Block attributes.
         * @param int   $post_id    Event post ID.
         */
        $data = apply_filters(
            'nemtly_booking_block_event_location_data',
            [
                'location' => $location,
                'icon'     => $icon,
            ],
            $attributes,
            $post_id
        );

        $output = sprintf(
            '<div %1$s>%2$s<span class="nemtly-location-text">%3$s</span></div>',
            $wrapper_attributes,
            $data['icon'],
            esc_html( $data['location'] )
        );

        /**
         * Filter the final rendered HTML of the Event Location block.
         *
         * @since PBOOKING_SINCE
         *
         * @param string $output     Rendered block HTML.
         * @param array  $data       Render data used to build the output.
         * @param array  $attributes Block attributes.
         * @param int    $post_id    Event post ID.
         */
        return apply_filters( 'nemtly_booking_block_event_location_html', $output, $data, $attributes, $post_id );
    }
}
