/* eslint-disable @typescript-eslint/ban-types */
import { EnvironmentKind, ProjectFolderKind, ProjectRequestKind } from '@api-client/core/build/browser.js';
import { rename, close, folder, environment, request, deleteFile, send } from '../../ui/icons/Icons.js';
import { SplitCloseDirection } from '../../elements/layout/type.js';
function findNavigation(element) {
    if (element.localName === 'project-navigation') {
        return element;
    }
    const root = element.getRootNode();
    return root.host;
}
function renameItem(init) {
    const { key } = init.target.dataset;
    if (!key) {
        return;
    }
    const nav = findNavigation(init.target);
    nav.edited = key;
}
const commands = [
    // 
    // Workspace tabs
    // 
    {
        target: '.layout-tab',
        label: 'Duplicate',
        enabled: (ctx) => {
            const { kind, key } = ctx.target.dataset;
            if (!kind || !key) {
                return false;
            }
            if (kind !== ProjectRequestKind) {
                return false;
            }
            return true;
        },
        execute: (init) => {
            const { kind, key } = init.target.dataset;
            if (!kind || !key) {
                return;
            }
            if (kind !== ProjectRequestKind) {
                return;
            }
            const project = init.store.get('project');
            const obj = project.findRequest(key);
            if (!obj) {
                return;
            }
            const callback = init.store.get('callback');
            const layout = init.store.get('layout');
            const parent = obj.getParent() || project;
            const cp = obj.clone();
            parent.addRequest(cp);
            layout.addItem({
                key: cp.key,
                kind,
                label: cp.info.renderLabel,
                icon: 'request'
            });
            callback({
                action: 'duplicate',
                key: cp.key,
                kind: obj.kind,
            });
        }
    },
    {
        target: '.layout-tab',
        label: '',
        type: 'separator',
    },
    {
        target: '.layout-tab',
        label: 'Close',
        icon: close,
        execute: (init) => {
            const { key, panel } = init.target.dataset;
            if (!key || !panel) {
                return;
            }
            const layout = init.store.get('layout');
            const parent = layout.findPanel(panel);
            if (!parent) {
                return;
            }
            parent.removeItem(key);
        }
    },
    {
        target: '.layout-tab',
        label: 'Close others',
        enabled: (ctx) => {
            const shadow = ctx.target.getRootNode();
            const all = shadow.querySelectorAll('.layout-tab');
            if (all.length === 1) {
                return false;
            }
            return true;
        },
        execute: (init) => {
            const { key, panel } = init.target.dataset;
            if (!key || !panel) {
                return;
            }
            const layout = init.store.get('layout');
            const parent = layout.findPanel(panel);
            if (!parent) {
                return;
            }
            parent.removeRelative(key, SplitCloseDirection.both);
        },
    },
    {
        target: '.layout-tab',
        label: 'Close on the right',
        enabled: (ctx) => {
            const next = ctx.target.nextElementSibling;
            if (!next) {
                return false;
            }
            return true;
        },
        execute: (init) => {
            const { key, panel } = init.target.dataset;
            if (!key || !panel) {
                return;
            }
            const layout = init.store.get('layout');
            const parent = layout.findPanel(panel);
            if (!parent) {
                return;
            }
            parent.removeRelative(key, SplitCloseDirection.right);
        },
    },
    {
        target: '.layout-tab',
        label: 'Close on the left',
        enabled: (ctx) => {
            const { index } = ctx.target.dataset;
            if (index === '0') {
                return false;
            }
            return true;
        },
        execute: (init) => {
            const { key, panel } = init.target.dataset;
            if (!key || !panel) {
                return;
            }
            const layout = init.store.get('layout');
            const parent = layout.findPanel(panel);
            if (!parent) {
                return;
            }
            parent.removeRelative(key, SplitCloseDirection.left);
        },
    },
    {
        target: '.layout-tab',
        label: '',
        type: 'separator',
    },
    {
        target: '.layout-tab',
        label: 'Save',
        enabled: (ctx) => {
            const { dirty } = ctx.target.dataset;
            if (dirty !== 'true') {
                return false;
            }
            return true;
        },
        execute: (init) => {
            const { key } = init.target.dataset;
            if (!key) {
                return;
            }
            const page = init.store.get('page');
            page.triggerSave(key);
        },
    },
    //
    // NAVIGATION
    //
    {
        target: "project-navigation",
        label: 'Add folder',
        title: 'Adds a new folder to the project',
        icon: folder,
        execute: (init) => {
            const project = init.store.get('project');
            const callback = init.store.get('callback');
            const created = project.addFolder();
            callback({
                action: 'add',
                key: created.key,
                kind: created.kind,
            });
        },
        visible: (init) => init.store.has('project'),
    },
    {
        target: "project-navigation",
        label: 'Add request',
        title: 'Adds a new HTTP request to the project',
        icon: request,
        execute: (init) => {
            const project = init.store.get('project');
            const callback = init.store.get('callback');
            const layout = init.store.get('layout');
            const created = project.addRequest('http://');
            layout.addItem({
                key: created.key,
                kind: created.kind,
                label: created.info.renderLabel,
                icon: 'request'
            });
            callback({
                action: 'add',
                key: created.key,
                kind: created.kind,
            });
        },
        visible: (init) => init.store.has('project'),
    },
    {
        target: "project-navigation",
        label: 'Add environment',
        title: 'Adds a new environment definition to the project',
        icon: environment,
        execute: (init) => {
            const project = init.store.get('project');
            const callback = init.store.get('callback');
            const layout = init.store.get('layout');
            const created = project.addEnvironment('New environment');
            layout.addItem({
                key: created.key,
                kind: created.kind,
                label: created.info.renderLabel,
                icon: 'environment'
            });
            callback({
                action: 'add',
                key: created.key,
                kind: created.kind,
            });
        },
        visible: (init) => init.store.has('project'),
    },
    {
        target: `project-navigation`,
        type: 'separator',
        label: '',
    },
    {
        target: `project-navigation`,
        label: 'Run project',
        icon: send,
        title: 'Runs all requests in the folder',
        execute(args) {
            const page = args.store.get('page');
            page.openRunner();
        },
    },
    // 
    // Navigation items
    // 
    // folders
    {
        target: `li[data-kind="${ProjectFolderKind}"]`,
        label: 'Add folder',
        icon: folder,
        execute: (init) => {
            const project = init.store.get('project');
            const callback = init.store.get('callback');
            const layout = init.store.get('layout');
            const key = init.target.dataset.key;
            const created = project.addFolder(undefined, { parent: key, });
            const nav = findNavigation(init.target);
            nav.edited = created.key;
            nav.openGroup(key);
            layout.requestNameUpdate(created.key);
            callback({
                action: 'add',
                key: created.key,
                kind: created.kind,
            });
        },
        visible: (init) => init.store.has('project'),
    },
    {
        target: `li[data-kind="${ProjectFolderKind}"]`,
        label: 'Add request',
        icon: request,
        execute: (init) => {
            const project = init.store.get('project');
            const callback = init.store.get('callback');
            const layout = init.store.get('layout');
            const key = init.target.dataset.key;
            const f = project.findFolder(key);
            if (f) {
                const created = f.addRequest('http://');
                const nav = findNavigation(init.target);
                nav.edited = created.key;
                nav.openGroup(key);
                layout.addItem({
                    key: created.key,
                    kind: created.kind,
                    label: created.info.renderLabel,
                    icon: 'request'
                });
                callback({
                    action: 'add',
                    key: created.key,
                    kind: created.kind,
                });
            }
        },
        visible: (init) => init.store.has('project'),
    },
    {
        target: `li[data-kind="${ProjectFolderKind}"]`,
        label: 'Add environment',
        title: 'Adds a new environment definition to the folder',
        icon: environment,
        execute: (init) => {
            const project = init.store.get('project');
            const callback = init.store.get('callback');
            const layout = init.store.get('layout');
            const key = init.target.dataset.key;
            const f = project.findFolder(key);
            if (f) {
                const created = f.addEnvironment('New environment');
                const nav = findNavigation(init.target);
                nav.edited = created.key;
                nav.openGroup(key);
                layout.addItem({
                    key: created.key,
                    kind: created.kind,
                    label: created.info.renderLabel,
                    icon: 'environment'
                });
                callback({
                    action: 'add',
                    key: created.key,
                    kind: created.kind,
                });
            }
        },
        visible: (init) => init.store.has('project'),
    },
    {
        target: `li[data-kind="${ProjectFolderKind}"]`,
        type: 'separator',
        label: '',
    },
    {
        target: `li[data-kind="${ProjectFolderKind}"]`,
        label: 'Run folder',
        icon: send,
        title: 'Runs all requests in the folder',
        execute(args) {
            const key = args.target.dataset.key;
            // const project = args.store.get('project');
            const page = args.store.get('page');
            page.openRunner(key);
            // Events.Navigation.App.runProjectRunner({ key: project.key, parent: key, workspace: page.workspace });
        },
    },
    {
        target: `li[data-kind="${ProjectFolderKind}"]`,
        type: 'separator',
        label: '',
    },
    {
        target: `li[data-kind="${ProjectFolderKind}"]`,
        label: 'Rename',
        icon: rename,
        title: 'Renames this folder',
        execute: renameItem,
    },
    {
        target: `li[data-kind="${ProjectFolderKind}"]`,
        label: 'Delete',
        icon: deleteFile,
        title: 'Deletes this folder',
        execute: (init) => {
            const project = init.store.get('project');
            const callback = init.store.get('callback');
            const layout = init.store.get('layout');
            const key = init.target.dataset.key;
            const removed = project.removeFolder(key);
            if (removed) {
                layout.removeByParent(key);
                callback({
                    action: 'delete',
                    key,
                    kind: removed.kind,
                    removed,
                });
            }
            else {
                throw new Error(`Folder not found in the project.`);
            }
        },
    },
    // requests
    {
        target: `li[data-kind="${ProjectRequestKind}"]`,
        label: 'Rename',
        icon: rename,
        title: 'Renames this request',
        execute: renameItem,
    },
    {
        target: `li[data-kind="${ProjectRequestKind}"]`,
        label: 'Duplicate',
        title: 'Makes a duplicate of this request',
        execute: (init) => {
            const key = init.target.dataset.key;
            const project = init.store.get('project');
            const obj = project.findRequest(key);
            if (!obj) {
                return;
            }
            const callback = init.store.get('callback');
            const layout = init.store.get('layout');
            const parent = obj.getParent() || project;
            const cp = obj.clone();
            const created = parent.addRequest(cp);
            layout.addItem({
                key: cp.key,
                kind: cp.kind,
                label: cp.info.renderLabel,
                icon: 'request'
            });
            callback({
                action: 'duplicate',
                key,
                kind: created.kind,
            });
        },
    },
    {
        target: `li[data-kind="${ProjectRequestKind}"]`,
        label: 'Delete',
        icon: deleteFile,
        title: 'Deletes this request',
        execute: (init) => {
            const project = init.store.get('project');
            const callback = init.store.get('callback');
            const key = init.target.dataset.key;
            const removed = project.removeRequest(key);
            if (removed) {
                callback({
                    action: 'delete',
                    key,
                    kind: removed.kind,
                    removed,
                });
            }
            else {
                throw new Error(`Request not found in the project.`);
            }
        },
    },
    // environments
    {
        target: `li[data-kind="${EnvironmentKind}"]`,
        label: 'Rename',
        icon: rename,
        title: 'Renames this environment',
        execute: renameItem,
    },
    {
        target: `li[data-kind="${EnvironmentKind}"]`,
        label: 'Delete',
        icon: deleteFile,
        title: 'Deletes this environment',
        execute: (init) => {
            const project = init.store.get('project');
            const callback = init.store.get('callback');
            const key = init.target.dataset.key;
            const parent = init.target.dataset.parent;
            const removed = project.removeEnvironment(key, { parent: parent !== project.key ? parent : undefined });
            const nav = findNavigation(init.target);
            nav.requestUpdate();
            if (removed) {
                callback({
                    action: 'delete',
                    key,
                    kind: removed.kind,
                    removed,
                });
            }
            else {
                throw new Error(`Request not found in the project.`);
            }
        },
    },
];
export default commands;
//# sourceMappingURL=HttpClientCommands.js.map