(function (window, $) {
    if (!$) return;

    $(function () {
        var i18n = (window.nnaxFmAjax && window.nnaxFmAjax.i18n) ? window.nnaxFmAjax.i18n : {};
        function t(key, fallback) {
            return (i18n && typeof i18n[key] === 'string' && i18n[key] !== '') ? i18n[key] : fallback;
        }

        (function () {
            // Create-folder modal UX helper:
            // focus + select title input when opening, clear it when closing.
            var modal = document.getElementById('nnaxfuf-create-folder-modal');
            var input = document.getElementById('nnaxfuf_folder_title_modal');

            if (!modal || !input || !window.UIkit) return;

            UIkit.util.on(modal, 'shown', function () {
                input.focus();
                input.select();
            });

            UIkit.util.on(modal, 'hidden', function () {
                input.value = '';
            });
        })();

        // Delete modal is shared by two submit variants (move children vs recursive delete).
        // Both hidden fields must get the clicked folder id.
        $(document).on('click', '.nnaxfuf-open-delete-folder-modal', function () {
            var folderId = $(this).data('nnaxfuf-folder-id') || '';
            var title = $(this).data('nnaxfuf-folder-title') || '–';

            $('#nnaxfuf_delete_folder_title').text(title);
            $('#nnaxfuf_delete_folder_id_move').val(folderId);
            $('#nnaxfuf_delete_folder_id_recursive').val(folderId);
        });

        // Move-folder modal prefill:
        // - show current title
        // - pass id
        // - disable selecting itself as target
        // - select current parent as default target
        $(document).on('click', '.nnaxfuf-open-move-folder-modal', function () {
            var folderId = $(this).data('nnaxfuf-folder-id') || '';
            var title = $(this).data('nnaxfuf-folder-title') || '–';
            var parentId = $(this).data('nnaxfuf-folder-parent-id');
            var parentValue = (parentId === undefined || parentId === null || parentId === '') ? '0' : String(parentId);

            $('#nnaxfuf_move_folder_title').text(title);
            $('#nnaxfuf_move_folder_id').val(folderId);

            var $targetSelect = $('#nnaxfuf_target_folder_id');
            if ($targetSelect.length) {
                $targetSelect.find('option').prop('disabled', false);
                $targetSelect.find('option[value="' + String(folderId) + '"]').prop('disabled', true);
                $targetSelect.val(parentValue);
                if ($targetSelect.val() !== parentValue) {
                    $targetSelect.val('0');
                }
            }
        });

        // Move-file modal prefill mirrors folder move behavior, without self-disable logic.
        $(document).on('click', '.nnaxfuf-open-move-file-modal', function () {
            var fileId = $(this).data('nnaxfuf-file-id') || '';
            var title = $(this).data('nnaxfuf-file-title') || '–';
            var fileOwnerId = parseInt($(this).data('nnaxfuf-file-owner-id'), 10);
            var parentId = $(this).data('nnaxfuf-file-parent-id');
            var parentValue = (parentId === undefined || parentId === null || parentId === '') ? '0' : String(parentId);

            $('#nnaxfuf_move_file_title').text(title);
            $('#nnaxfuf_move_file_id').val(fileId);
            $('#nnaxfuf_move_file_modal_file_owner_id').val(Number.isFinite(fileOwnerId) ? String(fileOwnerId) : '0');

            var $targetSelect = $('#nnaxfuf_move_file_target_folder_id');
            if ($targetSelect.length) {
                $targetSelect.val(parentValue);
                if ($targetSelect.val() !== parentValue) {
                    $targetSelect.val('0');
                }
            }
        });

        // Warn owners before moving their own file into a folder owned by someone else.
        $(document).on('submit', '#nnaxfuf-move-file-modal form', function (e) {
            var $form = $(this);
            if ($form.data('nnaxfuf-confirmed-submit') === true) {
                $form.data('nnaxfuf-confirmed-submit', false);
                return;
            }

            var currentUserId = parseInt((window.nnaxFmAjax && window.nnaxFmAjax.current_user_id) ? window.nnaxFmAjax.current_user_id : 0, 10);
            var fileOwnerId = parseInt($('#nnaxfuf_move_file_modal_file_owner_id').val(), 10);
            var $targetOption = $('#nnaxfuf_move_file_target_folder_id option:selected');
            var targetFolderId = parseInt(($targetOption.val() || '0'), 10);
            var targetOwnerId = parseInt(($targetOption.data('owner-id') || 0), 10);

            if (!Number.isFinite(currentUserId) || currentUserId <= 0) return;
            if (!Number.isFinite(fileOwnerId) || fileOwnerId <= 0) return;
            if (!Number.isFinite(targetFolderId) || targetFolderId <= 0) return;
            if (!Number.isFinite(targetOwnerId) || targetOwnerId <= 0) return;
            if (fileOwnerId !== currentUserId) return;
            if (targetOwnerId === currentUserId) return;

            e.preventDefault();

            var message = t('move_file_foreign_owner_confirm', 'If you move this file into a folder owned by someone else, you may lose ownership rights if you later lose access to that folder. Continue?');
            var confirmLabel = t('move_file_foreign_owner_confirm_ok', 'Yes, move file');
            var cancelLabel = t('move_file_foreign_owner_confirm_cancel', 'Cancel');

            var confirmWithUIKit = window.NUFM && window.NUFM.confirmWithUIKit
                ? window.NUFM.confirmWithUIKit
                : function (msg, onConfirm) { if (window.confirm(msg)) onConfirm(); };

            confirmWithUIKit(message, function () {
                $form.data('nnaxfuf-confirmed-submit', true);
                $form.trigger('submit');
            }, {
                confirmLabel: confirmLabel,
                cancelLabel: cancelLabel,
                confirmClass: 'uk-button uk-button-danger'
            });
        });
    });
})(window, window.jQuery);
