(function () {
    'use strict';

    if (window.__orderNotesInit) {
        return;
    }
    window.__orderNotesInit = true;

    function bindEdit(button) {
        if (button.dataset.bound === '1') return;
        button.dataset.bound = '1';

        button.addEventListener('click', function () {
            var noteEl = button.closest('[data-note-id]');
            if (!noteEl) return;
            var body = noteEl.querySelector('.js-order-note-body');
            var form = noteEl.querySelector('.js-order-note-edit-form');
            if (!body || !form) return;
            body.style.display = 'none';
            form.style.display = '';
            var textarea = form.querySelector('textarea[name="body"]');
            if (textarea) {
                textarea.focus();
                var len = textarea.value.length;
                try { textarea.setSelectionRange(len, len); } catch (e) { /* ignore */ }
            }
        });
    }

    function bindCancel(button) {
        if (button.dataset.bound === '1') return;
        button.dataset.bound = '1';

        button.addEventListener('click', function () {
            var noteEl = button.closest('[data-note-id]');
            if (!noteEl) return;
            var body = noteEl.querySelector('.js-order-note-body');
            var form = noteEl.querySelector('.js-order-note-edit-form');
            if (!body || !form) return;
            form.style.display = 'none';
            body.style.display = '';
        });
    }

    function bindDelete(form) {
        if (form.dataset.bound === '1') return;
        form.dataset.bound = '1';

        form.addEventListener('submit', function (event) {
            event.preventDefault();
            if (window.OrderProAlerts && typeof window.OrderProAlerts.confirm === 'function') {
                window.OrderProAlerts.confirm({
                    title: 'Usunac notatke?',
                    message: 'Tej operacji nie mozna cofnac.',
                    danger: true,
                    confirmLabel: 'Usun',
                    onConfirm: function () {
                        form.dataset.bound = '2';
                        form.submit();
                    }
                });
            } else {
                form.dataset.bound = '2';
                form.submit();
            }
        });
    }

    function init() {
        var editButtons = document.querySelectorAll('.js-order-note-edit');
        for (var i = 0; i < editButtons.length; i++) {
            bindEdit(editButtons[i]);
        }
        var cancelButtons = document.querySelectorAll('.js-order-note-edit-cancel');
        for (var j = 0; j < cancelButtons.length; j++) {
            bindCancel(cancelButtons[j]);
        }
        var deleteForms = document.querySelectorAll('.js-order-note-delete');
        for (var k = 0; k < deleteForms.length; k++) {
            bindDelete(deleteForms[k]);
        }
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', init);
    } else {
        init();
    }
})();
