function encodePickedPath(p) {
    if (p === '/' || p === '') return '/';
    return '/' + p.replace(/^\/+/, '').split('/').map(encodeURIComponent).join('/');
}

function promptPageName(title, initial, confirmLabel, onName) {
    var input = document.createElement('input');
    input.type = 'text';
    input.className = 'modal-input';
    input.placeholder = 'Page name';
    if (initial) input.value = initial;
    openModal({
        title: title,
        body: input,
        confirm: {
            label: confirmLabel,
            onConfirm: function () {
                var name = input.value.trim();
                if (!name) return;
                onName(name);
            }
        }
    });
}

function newPage() {
    var current = decodeURIComponent(window.location.pathname).replace(/\/+$/, '') || '/';
    openTreePicker({
        title: 'New page — where?',
        mode: 'folder',
        initialPath: current,
        preselect: current,
        hideFiles: true,
        confirmLabel: 'NEXT',
        onSelect: function (parentPath) {
            promptPageName('New page — name?', '', 'CREATE', function (name) {
                var base = parentPath === '/' ? '/' : encodePickedPath(parentPath) + '/';
                window.location.href = base + encodeURIComponent(name) + '/?edit';
            });
        }
    });
}

function movePage() {
    var current = decodeURIComponent(window.location.pathname).replace(/\/+$/, '');
    if (!current) return;
    var segs = current.split('/').filter(Boolean);
    var currentName = segs[segs.length - 1] || '';
    var parent = '/' + segs.slice(0, -1).join('/');
    if (parent === '/') parent = '/';

    openTreePicker({
        title: 'Move — new parent?',
        mode: 'folder',
        initialPath: parent,
        preselect: parent,
        hideFiles: true,
        confirmLabel: 'NEXT',
        onSelect: function (newParent) {
            var input = document.createElement('input');
            input.type = 'text';
            input.className = 'modal-input';
            input.placeholder = 'Page name';
            input.value = currentName;

            var linksCheckbox = document.createElement('input');
            linksCheckbox.type = 'checkbox';
            linksCheckbox.id = 'move-update-links';

            var linksLabel = document.createElement('label');
            linksLabel.htmlFor = linksCheckbox.id;
            linksLabel.className = 'modal-checkbox';
            linksLabel.appendChild(linksCheckbox);
            linksLabel.appendChild(document.createTextNode('Update links'));

            var body = document.createDocumentFragment();
            body.appendChild(input);
            body.appendChild(linksLabel);

            openModal({
                title: 'Move — new name?',
                body: body,
                confirm: {
                    label: 'MOVE',
                    onConfirm: function () {
                        var name = input.value.trim();
                        if (!name) return;
                        var dest = (newParent === '/' ? '' : newParent) + '/' + name;
                        var action = window.location.pathname + '?move=' +
                            encodeURIComponent(dest);
                        if (linksCheckbox.checked) action += '&links=1';
                        var form = document.createElement('form');
                        form.method = 'POST';
                        form.action = action;
                        document.body.appendChild(form);
                        form.submit();
                    }
                }
            });
        }
    });
}

function deletePage() {
    var decodedPath = decodeURIComponent(window.location.pathname);

    openModal({
        title: 'Delete page',
        body: 'Delete ' + decodedPath + ' and everything inside it?',
        confirm: {
            label: 'DELETE',
            danger: true,
            enterConfirms: false,
            onConfirm: function () {
                var form = document.createElement('form');
                form.method = 'POST';
                form.action = window.location.pathname + '?delete=1';
                document.body.appendChild(form);
                form.submit();
            }
        },
        cancel: { autofocus: true },
        swapButtons: true
    });
}

document.addEventListener('DOMContentLoaded', function () {
    wireDropdown(document.querySelector('[data-action="actions-drop"]'));
});
