import { localize2WithPath, localizeWithPath } from '../../../nls.js';
import { hasWorkspaceFileExtension, WorkbenchState } from '../../../platform/workspace/common/workspace.js';
import { IWorkspaceContextService } from '../../../platform/workspace/common/workspace.service.js';
import { IWorkspaceEditingService } from '../../services/workspaces/common/workspaceEditing.service.js';
import { IEditorService } from '../../services/editor/common/editorService.service.js';
import { ICommandService } from '../../../platform/commands/common/commands.service.js';
import { SET_ROOT_FOLDER_COMMAND_ID, ADD_ROOT_FOLDER_LABEL, ADD_ROOT_FOLDER_COMMAND_ID, PICK_WORKSPACE_FOLDER_COMMAND_ID } from './workspaceCommands.js';
import { IFileDialogService } from '../../../platform/dialogs/common/dialogs.service.js';
import { Action2, registerAction2, MenuRegistry, MenuId } from '../../../platform/actions/common/actions.js';
import { OpenFolderWorkspaceSupportContext, WorkbenchStateContext, EnterMultiRootWorkspaceSupportContext, EmptyWorkspaceSupportContext, WorkspaceFolderCountContext } from '../../common/contextkeys.js';
import { IHostService } from '../../services/host/browser/host.service.js';
import { KeyMod as KeyMod$1, KeyCode, KeyChord } from '../../../base/common/keyCodes.js';
import { ContextKeyExpr } from '../../../platform/contextkey/common/contextkey.js';
import { IWorkbenchEnvironmentService } from '../../services/environment/common/environmentService.service.js';
import { IWorkspacesService } from '../../../platform/workspaces/common/workspaces.service.js';
import { KeybindingWeight } from '../../../platform/keybinding/common/keybindingsRegistry.js';
import { IsMacNativeContext } from '../../../platform/contextkey/common/contextkeys.js';
import { Categories } from '../../../platform/action/common/actionCommonCategories.js';
const _moduleId = "vs/workbench/browser/actions/workspaceActions";
const workspacesCategory = ( localize2WithPath(_moduleId, 0, 'Workspaces'));
class OpenFileAction extends Action2 {
    static { this.ID = 'workbench.action.files.openFile'; }
    constructor() {
        super({
            id: OpenFileAction.ID,
            title: ( localize2WithPath(_moduleId, 1, 'Open File...')),
            category: Categories.File,
            f1: true,
            keybinding: {
                when: ( (IsMacNativeContext.toNegated())),
                weight: KeybindingWeight.WorkbenchContrib,
                primary: KeyMod$1.CtrlCmd | KeyCode.KeyO
            }
        });
    }
    async run(accessor, data) {
        const fileDialogService = accessor.get(IFileDialogService);
        return fileDialogService.pickFileAndOpen({ forceNewWindow: false, telemetryExtraData: data });
    }
}
class OpenFolderAction extends Action2 {
    static { this.ID = 'workbench.action.files.openFolder'; }
    constructor() {
        super({
            id: OpenFolderAction.ID,
            title: ( localize2WithPath(_moduleId, 2, 'Open Folder...')),
            category: Categories.File,
            f1: true,
            precondition: OpenFolderWorkspaceSupportContext,
            keybinding: {
                weight: KeybindingWeight.WorkbenchContrib,
                primary: undefined,
                linux: {
                    primary: KeyChord(KeyMod$1.CtrlCmd | KeyCode.KeyK, KeyMod$1.CtrlCmd | KeyCode.KeyO)
                },
                win: {
                    primary: KeyChord(KeyMod$1.CtrlCmd | KeyCode.KeyK, KeyMod$1.CtrlCmd | KeyCode.KeyO)
                }
            }
        });
    }
    async run(accessor, data) {
        const fileDialogService = accessor.get(IFileDialogService);
        return fileDialogService.pickFolderAndOpen({ forceNewWindow: false, telemetryExtraData: data });
    }
}
class OpenFolderViaWorkspaceAction extends Action2 {
    static { this.ID = 'workbench.action.files.openFolderViaWorkspace'; }
    constructor() {
        super({
            id: OpenFolderViaWorkspaceAction.ID,
            title: ( localize2WithPath(_moduleId, 2, 'Open Folder...')),
            category: Categories.File,
            f1: true,
            precondition: ( (ContextKeyExpr.and(
                 (OpenFolderWorkspaceSupportContext.toNegated()),
                 (WorkbenchStateContext.isEqualTo('workspace'))
            ))),
            keybinding: {
                weight: KeybindingWeight.WorkbenchContrib,
                primary: KeyMod$1.CtrlCmd | KeyCode.KeyO
            }
        });
    }
    run(accessor) {
        const commandService = accessor.get(ICommandService);
        return commandService.executeCommand(SET_ROOT_FOLDER_COMMAND_ID);
    }
}
class OpenFileFolderAction extends Action2 {
    static { this.ID = 'workbench.action.files.openFileFolder'; }
    static { this.LABEL = ( localize2WithPath(_moduleId, 3, 'Open...')); }
    constructor() {
        super({
            id: OpenFileFolderAction.ID,
            title: OpenFileFolderAction.LABEL,
            category: Categories.File,
            f1: true,
            precondition: ( (ContextKeyExpr.and(IsMacNativeContext, OpenFolderWorkspaceSupportContext))),
            keybinding: {
                weight: KeybindingWeight.WorkbenchContrib,
                primary: KeyMod$1.CtrlCmd | KeyCode.KeyO
            }
        });
    }
    async run(accessor, data) {
        const fileDialogService = accessor.get(IFileDialogService);
        return fileDialogService.pickFileFolderAndOpen({ forceNewWindow: false, telemetryExtraData: data });
    }
}
class OpenWorkspaceAction extends Action2 {
    static { this.ID = 'workbench.action.openWorkspace'; }
    constructor() {
        super({
            id: OpenWorkspaceAction.ID,
            title: ( localize2WithPath(_moduleId, 4, 'Open Workspace from File...')),
            category: Categories.File,
            f1: true,
            precondition: EnterMultiRootWorkspaceSupportContext
        });
    }
    async run(accessor, data) {
        const fileDialogService = accessor.get(IFileDialogService);
        return fileDialogService.pickWorkspaceAndOpen({ telemetryExtraData: data });
    }
}
class CloseWorkspaceAction extends Action2 {
    static { this.ID = 'workbench.action.closeFolder'; }
    constructor() {
        super({
            id: CloseWorkspaceAction.ID,
            title: ( localize2WithPath(_moduleId, 5, 'Close Workspace')),
            category: workspacesCategory,
            f1: true,
            precondition: ( (ContextKeyExpr.and(
                 (WorkbenchStateContext.notEqualsTo('empty')),
                EmptyWorkspaceSupportContext
            ))),
            keybinding: {
                weight: KeybindingWeight.WorkbenchContrib,
                primary: KeyChord(KeyMod$1.CtrlCmd | KeyCode.KeyK, KeyCode.KeyF)
            }
        });
    }
    async run(accessor) {
        const hostService = accessor.get(IHostService);
        const environmentService = accessor.get(IWorkbenchEnvironmentService);
        return hostService.openWindow({ forceReuseWindow: true, remoteAuthority: environmentService.remoteAuthority });
    }
}
class OpenWorkspaceConfigFileAction extends Action2 {
    static { this.ID = 'workbench.action.openWorkspaceConfigFile'; }
    constructor() {
        super({
            id: OpenWorkspaceConfigFileAction.ID,
            title: ( localize2WithPath(_moduleId, 6, 'Open Workspace Configuration File')),
            category: workspacesCategory,
            f1: true,
            precondition: ( (WorkbenchStateContext.isEqualTo('workspace')))
        });
    }
    async run(accessor) {
        const contextService = accessor.get(IWorkspaceContextService);
        const editorService = accessor.get(IEditorService);
        const configuration = contextService.getWorkspace().configuration;
        if (configuration) {
            await editorService.openEditor({ resource: configuration, options: { pinned: true } });
        }
    }
}
class AddRootFolderAction extends Action2 {
    static { this.ID = 'workbench.action.addRootFolder'; }
    constructor() {
        super({
            id: AddRootFolderAction.ID,
            title: ADD_ROOT_FOLDER_LABEL,
            category: workspacesCategory,
            f1: true,
            precondition: ( (ContextKeyExpr.or(
                EnterMultiRootWorkspaceSupportContext,
                 (WorkbenchStateContext.isEqualTo('workspace'))
            )))
        });
    }
    run(accessor) {
        const commandService = accessor.get(ICommandService);
        return commandService.executeCommand(ADD_ROOT_FOLDER_COMMAND_ID);
    }
}
class RemoveRootFolderAction extends Action2 {
    static { this.ID = 'workbench.action.removeRootFolder'; }
    constructor() {
        super({
            id: RemoveRootFolderAction.ID,
            title: ( localize2WithPath(_moduleId, 7, 'Remove Folder from Workspace...')),
            category: workspacesCategory,
            f1: true,
            precondition: ( (ContextKeyExpr.and(
                 (WorkspaceFolderCountContext.notEqualsTo('0')),
                 (ContextKeyExpr.or(
                    EnterMultiRootWorkspaceSupportContext,
                     (WorkbenchStateContext.isEqualTo('workspace'))
                ))
            )))
        });
    }
    async run(accessor) {
        const commandService = accessor.get(ICommandService);
        const workspaceEditingService = accessor.get(IWorkspaceEditingService);
        const folder = await commandService.executeCommand(PICK_WORKSPACE_FOLDER_COMMAND_ID);
        if (folder) {
            await workspaceEditingService.removeFolders([folder.uri]);
        }
    }
}
class SaveWorkspaceAsAction extends Action2 {
    static { this.ID = 'workbench.action.saveWorkspaceAs'; }
    constructor() {
        super({
            id: SaveWorkspaceAsAction.ID,
            title: ( localize2WithPath(_moduleId, 8, 'Save Workspace As...')),
            category: workspacesCategory,
            f1: true,
            precondition: EnterMultiRootWorkspaceSupportContext
        });
    }
    async run(accessor) {
        const workspaceEditingService = accessor.get(IWorkspaceEditingService);
        const contextService = accessor.get(IWorkspaceContextService);
        const configPathUri = await workspaceEditingService.pickNewWorkspacePath();
        if (configPathUri && hasWorkspaceFileExtension(configPathUri)) {
            switch (contextService.getWorkbenchState()) {
                case WorkbenchState.EMPTY:
                case WorkbenchState.FOLDER: {
                    const folders = ( (contextService.getWorkspace().folders.map(folder => ({ uri: folder.uri }))));
                    return workspaceEditingService.createAndEnterWorkspace(folders, configPathUri);
                }
                case WorkbenchState.WORKSPACE:
                    return workspaceEditingService.saveAndEnterWorkspace(configPathUri);
            }
        }
    }
}
class DuplicateWorkspaceInNewWindowAction extends Action2 {
    static { this.ID = 'workbench.action.duplicateWorkspaceInNewWindow'; }
    constructor() {
        super({
            id: DuplicateWorkspaceInNewWindowAction.ID,
            title: ( localize2WithPath(_moduleId, 9, 'Duplicate As Workspace in New Window')),
            category: workspacesCategory,
            f1: true,
            precondition: EnterMultiRootWorkspaceSupportContext
        });
    }
    async run(accessor) {
        const workspaceContextService = accessor.get(IWorkspaceContextService);
        const workspaceEditingService = accessor.get(IWorkspaceEditingService);
        const hostService = accessor.get(IHostService);
        const workspacesService = accessor.get(IWorkspacesService);
        const environmentService = accessor.get(IWorkbenchEnvironmentService);
        const folders = workspaceContextService.getWorkspace().folders;
        const remoteAuthority = environmentService.remoteAuthority;
        const newWorkspace = await workspacesService.createUntitledWorkspace(folders, remoteAuthority);
        await workspaceEditingService.copyWorkspaceSettings(newWorkspace);
        return hostService.openWindow([{ workspaceUri: newWorkspace.configPath }], { forceNewWindow: true, remoteAuthority });
    }
}
registerAction2(AddRootFolderAction);
registerAction2(RemoveRootFolderAction);
registerAction2(OpenFileAction);
registerAction2(OpenFolderAction);
registerAction2(OpenFolderViaWorkspaceAction);
registerAction2(OpenFileFolderAction);
registerAction2(OpenWorkspaceAction);
registerAction2(OpenWorkspaceConfigFileAction);
registerAction2(CloseWorkspaceAction);
registerAction2(SaveWorkspaceAsAction);
registerAction2(DuplicateWorkspaceInNewWindowAction);
MenuRegistry.appendMenuItem(MenuId.MenubarFileMenu, {
    group: '2_open',
    command: {
        id: OpenFileAction.ID,
        title: ( localizeWithPath(_moduleId, 10, "&&Open File..."))
    },
    order: 1,
    when: ( (IsMacNativeContext.toNegated()))
});
MenuRegistry.appendMenuItem(MenuId.MenubarFileMenu, {
    group: '2_open',
    command: {
        id: OpenFolderAction.ID,
        title: ( localizeWithPath(_moduleId, 11, "Open &&Folder..."))
    },
    order: 2,
    when: OpenFolderWorkspaceSupportContext
});
MenuRegistry.appendMenuItem(MenuId.MenubarFileMenu, {
    group: '2_open',
    command: {
        id: OpenFolderViaWorkspaceAction.ID,
        title: ( localizeWithPath(_moduleId, 11, "Open &&Folder..."))
    },
    order: 2,
    when: ( (ContextKeyExpr.and(
         (OpenFolderWorkspaceSupportContext.toNegated()),
         (WorkbenchStateContext.isEqualTo('workspace'))
    )))
});
MenuRegistry.appendMenuItem(MenuId.MenubarFileMenu, {
    group: '2_open',
    command: {
        id: OpenFileFolderAction.ID,
        title: ( localizeWithPath(_moduleId, 12, "&&Open..."))
    },
    order: 1,
    when: ( (ContextKeyExpr.and(IsMacNativeContext, OpenFolderWorkspaceSupportContext)))
});
MenuRegistry.appendMenuItem(MenuId.MenubarFileMenu, {
    group: '2_open',
    command: {
        id: OpenWorkspaceAction.ID,
        title: ( localizeWithPath(_moduleId, 13, "Open Wor&&kspace from File..."))
    },
    order: 3,
    when: EnterMultiRootWorkspaceSupportContext
});
MenuRegistry.appendMenuItem(MenuId.MenubarFileMenu, {
    group: '3_workspace',
    command: {
        id: ADD_ROOT_FOLDER_COMMAND_ID,
        title: ( localizeWithPath(_moduleId, 14, "A&&dd Folder to Workspace..."))
    },
    when: ( (ContextKeyExpr.or(
        EnterMultiRootWorkspaceSupportContext,
         (WorkbenchStateContext.isEqualTo('workspace'))
    ))),
    order: 1
});
MenuRegistry.appendMenuItem(MenuId.MenubarFileMenu, {
    group: '3_workspace',
    command: {
        id: SaveWorkspaceAsAction.ID,
        title: ( localizeWithPath(_moduleId, 15, "Save Workspace As..."))
    },
    order: 2,
    when: EnterMultiRootWorkspaceSupportContext
});
MenuRegistry.appendMenuItem(MenuId.MenubarFileMenu, {
    group: '3_workspace',
    command: {
        id: DuplicateWorkspaceInNewWindowAction.ID,
        title: ( localizeWithPath(_moduleId, 16, "Duplicate Workspace"))
    },
    order: 3,
    when: EnterMultiRootWorkspaceSupportContext
});
MenuRegistry.appendMenuItem(MenuId.MenubarFileMenu, {
    group: '6_close',
    command: {
        id: CloseWorkspaceAction.ID,
        title: ( localizeWithPath(_moduleId, 17, "Close &&Folder"))
    },
    order: 3,
    when: ( (ContextKeyExpr.and(
         (WorkbenchStateContext.isEqualTo('folder')),
        EmptyWorkspaceSupportContext
    )))
});
MenuRegistry.appendMenuItem(MenuId.MenubarFileMenu, {
    group: '6_close',
    command: {
        id: CloseWorkspaceAction.ID,
        title: ( localizeWithPath(_moduleId, 18, "Close &&Workspace"))
    },
    order: 3,
    when: ( (ContextKeyExpr.and(
         (WorkbenchStateContext.isEqualTo('workspace')),
        EmptyWorkspaceSupportContext
    )))
});
export { AddRootFolderAction, OpenFileAction, OpenFileFolderAction, OpenFolderAction, OpenFolderViaWorkspaceAction, RemoveRootFolderAction };
