<?xml version="1.0"?>
<!--
  El Ganso (Hyvä) — Magewire init en handle hyva_checkout_index_index

  Why: Magewire registra el bloque `magewire.initialize` (que define
  `window.Magewire = window.magewire` y arranca `new Livewire()`) SOLO en
  el handle `default_hyva.xml`. El page_type `hyva_checkout_index_index`
  no extiende `default_hyva` (es un layout independiente con
  layout="1column"), así que window.Magewire NUNCA se define en /checkout/
  por defecto y Hyvä Checkout falla con "Magewire is not defined" en cada
  componente Magewire (Stripe Express Checkout, CouponCode, PriceSummary...).

  Replicamos aquí la estructura exacta de
  vendor/magewirephp/magewire/src/view/frontend/layout/default_hyva.xml
  (sección before.body.end) para que magewire.livewire + magewire.initialize
  se rendericen también en el handle del checkout. Los containers internos
  (magewire.plugin.scripts, magewire.directive.scripts, magewire.extend.scripts)
  son referenciados por hyva_checkout.xml y hyva_checkout_index_index.xml del
  vendor Hyvä Checkout, así que también deben existir aquí.

  Doc ref: vendor/magewirephp/magewire/src/view/frontend/layout/default_hyva.xml
  Doc Hyvä: https://docs.hyva.io/checkout/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <!--
          NOTA 2026-05-09: livewire.min.js DEBE cargar en el <head> para que
          window.Livewire esté disponible antes de cualquier script inline en
          el body que use Magewire (Hyvä Checkout, Stripe Express, etc.).
          page_configuration.xsd permite <script src=> directo en <head>.
        -->
        <script src="Magewirephp_Magewire::js/livewire.js" src_type="default"/>
    </head>

    <body>
        <!--
          El theme quita Magewire del handle `default` (catálogo sin livewire.js).
          Magento guarda esos nombres en la remove-list: sin remove="false" aquí,
          el bloque de abajo se descarta y checkout queda con "Magewire is not defined"
          + Alpine roto (visible / expand / Stripe Express).
        -->
        <referenceBlock name="magewire.livewire" remove="false"/>
        <referenceBlock name="magewire.initialize" remove="false"/>
        <referenceBlock name="magewire.loader" remove="false"/>

        <!--
          NOTA 2026-05-09: magewire.initialize VUELVE a before.body.end (no
          after.body.start). Razón: dispatch del evento `magewire:available`
          DEBE ocurrir DESPUÉS de que init-config.phtml/init-payment.phtml
          (de Hyva Checkout) hayan registrado sus listeners. Esos templates
          están en blocks del cuerpo principal del checkout. Si dispatch
          ocurre antes de que registren el listener, hyvaCheckout.config.config
          nunca se setea → registerMethod falla con "Payment method code must
          be a non-empty string".

          Livewire está en <head> (cargado y disponible). magewire.initialize
          va al final del body para dispatch tardío.
        -->
        <referenceContainer name="before.body.end">
            <block name="magewire.initialize"
                   template="Magewirephp_Magewire::page/js/magewire-initialize.phtml">
                <arguments>
                    <argument name="view_model" xsi:type="object">Magewirephp\Magewire\ViewModel\Magewire</argument>
                </arguments>

                <container name="magewire.plugin.scripts" as="magewire.plugins">
                    <block name="magewire.plugin.loader"
                           ifconfig="dev/magewire/loader/enable"
                           template="Magewirephp_Magewire::page/js/magewire/plugin/loader.phtml">
                        <arguments>
                            <argument name="view_model" xsi:type="object">Magewirephp\Magewire\ViewModel\Magewire</argument>
                        </arguments>
                    </block>

                    <block name="magewire.plugin.error"
                           template="Magewirephp_Magewire::page/js/magewire/plugin/error.phtml">
                        <arguments>
                            <argument name="view_model" xsi:type="object">Magewirephp\Magewire\ViewModel\Magewire</argument>
                            <argument name="status_messages" xsi:type="array">
                                <item name="419" xsi:type="string">
                                    The session has expired. Therefore, the page will be refreshed when you confirm.
                                </item>
                            </argument>
                        </arguments>
                    </block>

                    <!--
                      Hyvä Checkout intenta declarar este block via
                      <referenceContainer name="magewire.plugin.scripts"> en su
                      hyva_checkout.xml, pero su XML carga ANTES que la declaración
                      del container `magewire.plugin.scripts` (que vive aquí dentro
                      del block magewire.initialize de mi override). Resultado:
                      generateElements() descarta el block "huérfano" → la función
                      hyvaCheckoutMagewirePluginError nunca se registra en Alpine.
                      Lo declaramos aquí explícitamente para evitar el race.
                    -->
                    <block name="hyva-checkout.page.js.magewire.plugin.error"
                           after="magewire.plugin.error"
                           template="Hyva_Checkout::page/js/magewire/plugin/error.phtml"/>

                    <!--
                      Mismo caso con Hyva_CheckoutPayPal (su hyva_checkout.xml hace
                      referenceContainer a este container y se descarta): sin estos
                      scripts no se define PaypalExpressInContext, no carga el SDK
                      de PayPal y no salen los botones. Copia literal de
                      vendor/hyva-themes/magento2-hyva-checkout-paypal/src/view/frontend/layout/hyva_checkout.xml
                      (revisar al actualizar el módulo).
                    -->
                    <block name="checkout.payment.credit_card_form"
                           class="Magento\Payment\Block\Form\Cc"
                           template="Hyva_CheckoutPayPal::checkout/payment/credit_card_form-csp-js.phtml"/>
                    <block name="checkout.payment.method.payflow_iframe.iframe"
                           template="Hyva_CheckoutPayPal::checkout/payment/method/payflow_iframe/iframe-csp-js.phtml"/>
                    <block name="checkout.payment.method.payflowpro.iframe_hidden_form"
                           template="Hyva_CheckoutPayPal::checkout/payment/method/payflowpro/iframe_hidden_form-csp-js.phtml"/>
                    <block name="checkout.payment.method.payflowpro.payflowpro_form"
                           template="Hyva_CheckoutPayPal::checkout/payment/method/payflowpro/payflowpro_form-csp-js.phtml"/>
                    <block name="checkout.payment.method.payflowpro.recaptcha"
                           template="Hyva_CheckoutPayPal::checkout/payment/method/payflowpro/recaptcha-csp-js.phtml"/>
                    <block name="checkout.payment.method.paypal_express.pay_later"
                           template="Hyva_CheckoutPayPal::checkout/payment/method/paypal_express/pay_later-csp-js.phtml"/>
                    <block name="checkout.payment.method.paypal_express.paypal_express_in_context"
                           template="Hyva_CheckoutPayPal::checkout/payment/method/paypal_express/paypal_express_in_context-csp-js.phtml"/>
                </container>

                <container name="magewire.directive.scripts" as="magewire.directives">
                    <block name="magewire.directive.select"
                           template="Magewirephp_Magewire::page/js/magewire/directive/select.phtml">
                        <arguments>
                            <argument name="view_model" xsi:type="object">Magewirephp\Magewire\ViewModel\Magewire</argument>
                        </arguments>
                    </block>
                </container>

                <container name="magewire.extend.scripts" as="magewire.extends">
                    <block name="magewire.extend.plugin"
                           template="Magewirephp_Magewire::page/js/magewire/extend/plugin.phtml">
                        <arguments>
                            <argument name="view_model" xsi:type="object">Magewirephp\Magewire\ViewModel\Magewire</argument>
                        </arguments>
                    </block>
                </container>
            </block>
        </referenceContainer>
    </body>
</page>

