define([
    'jquery',
    'mage/utils/wrapper'
], function ($, wrapper) {
    'use strict';

    /**
     * Move the Shipping Information fieldset (date + instructions) before
     * the actions-toolbar (Next button) so it appears above the Next button.
     */
    function reorderShippingFields() {
        var $step = $('#checkout-step-shipping');
        if (!$step.length) {
            return;
        }
        var $actionsToolbar = $step.find('.actions-toolbar').first();
        var $shippingInfo   = $step.find('.fieldset.shipping-information').first();

        if ($actionsToolbar.length && $shippingInfo.length) {
            // Only move if currently after the toolbar
            if ($actionsToolbar.index() < $shippingInfo.index() || $shippingInfo.closest('.actions-toolbar').length === 0) {
                $shippingInfo.insertBefore($actionsToolbar);
                console.log('✅ SHIPPING FIELDS: Moved Shipping Information before Next button');
            }
        }
    }

    return function (targetComponent) {
        return wrapper.extend(targetComponent, {
            initialize: function () {
                this._super();
                // Wait for KO to finish rendering the step
                var self = this;
                setTimeout(function () {
                    reorderShippingFields();
                }, 300);
                return self;
            }
        });
    };
});
