<?php
declare(strict_types=1);

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

/**
 * Form block: one palette entry for every lead-capture form.
 *
 * Contact, Booking and Quote were three separate blocks doing the same job
 * (collect fields, validate, store a record, notify), which meant three chips
 * in the palette and three places to learn. This block carries a `form_type`
 * setting and routes to the renderer that already owns each behaviour, so the
 * output is byte-identical to the old blocks and nothing about deposits,
 * estimate numbering or notification wiring changes.
 *
 * Deliberately NOT merged: login and register. They are auth flows with their
 * own validation, session handling and gating, not lead capture; folding them
 * into a "pick your form" chip would put account creation one dropdown away
 * from a contact form.
 *
 * Existing pages keep working untouched: the contact / booking / estimate types
 * still render through their own cases, so nothing needs migrating. New pages
 * get one Form chip instead of three.
 *
 * `quote` maps to the estimate renderer because they are the same concept named
 * twice: the vertical sites' "request a quote" and the native estimate block.
 *
 * @since 2.1.2847
 */
if (!function_exists('impulse_element_render_form')) {
    /** Map a form_type to the renderer that owns it. */
    function impulse_form_type_target(string $type): string
    {
        $map = [
            'contact' => 'contact',
            'booking' => 'booking',
            'quote'   => 'estimate',
        ];
        return $map[$type] ?? 'contact';
    }

    function impulse_element_render_form($el, array $settings, string $context): string
    {
        $type   = (string) ($settings['form_type'] ?? 'contact');
        $target = impulse_form_type_target($type);
        $fn     = 'impulse_element_render_' . $target;

        // The estimate renderer is a VERTICAL_FILE and is stripped from the free
        // WP build. Fall back to contact rather than fatal, so a page authored on
        // a vertical site still renders something usable if it lands on a build
        // without the quote form.
        if (!function_exists($fn)) {
            $fn = 'impulse_element_render_contact';
            if (!function_exists($fn)) return '';
        }
        return (string) $fn($el, $settings, $context);
    }
}
