import { __decorate, __param } from '../../../../../../../external/tslib/tslib.es6.js';
import './media/editorplaceholder.css.js';
import { localizeWithPath } from '../../../../nls.js';
import { truncate, truncateMiddle } from '../../../../base/common/strings.js';
import Severity$1 from '../../../../base/common/severity.js';
import { isEditorOpenError } from '../../../common/editor.js';
import { EditorPane } from './editorPane.js';
import { ITelemetryService } from '../../../../platform/telemetry/common/telemetry.service.js';
import { DomScrollableElement } from '../../../../base/browser/ui/scrollbar/scrollableElement.js';
import { ScrollbarVisibility } from '../../../../base/common/scrollable.js';
import { IThemeService } from '../../../../platform/theme/common/themeService.service.js';
import { clearNode, $, EventHelper, size } from '../../../../base/browser/dom.js';
import { MutableDisposable, DisposableStore } from '../../../../base/common/lifecycle.js';
import { IStorageService } from '../../../../platform/storage/common/storage.service.js';
import { assertAllDefined } from '../../../../base/common/types.js';
import { ICommandService } from '../../../../platform/commands/common/commands.service.js';
import { isSingleFolderWorkspaceIdentifier, toWorkspaceIdentifier } from '../../../../platform/workspace/common/workspace.js';
import { IWorkspaceContextService } from '../../../../platform/workspace/common/workspace.service.js';
import { EditorOpenSource } from '../../../../platform/editor/common/editor.js';
import { computeEditorAriaLabel, EditorPaneDescriptor } from '../../editor.js';
import { ButtonBar } from '../../../../base/browser/ui/button/button.js';
import { defaultButtonStyles } from '../../../../platform/theme/browser/defaultStyles.js';
import { SimpleIconLabel } from '../../../../base/browser/ui/iconLabel/simpleIconLabel.js';
import { FileChangeType, FileOperationResult } from '../../../../platform/files/common/files.js';
import { IFileService } from '../../../../platform/files/common/files.service.js';
import { toErrorMessage } from '../../../../base/common/errorMessage.js';
import { IDialogService } from '../../../../platform/dialogs/common/dialogs.service.js';
var EditorPlaceholder_1, WorkspaceTrustRequiredPlaceholderEditor_1, ErrorPlaceholderEditor_1;
const _moduleId = "vs/workbench/browser/parts/editor/editorPlaceholder";
let EditorPlaceholder = class EditorPlaceholder extends EditorPane {
    static { EditorPlaceholder_1 = this; }
    static { this.PLACEHOLDER_LABEL_MAX_LENGTH = 1024; }
    constructor(id, group, telemetryService, themeService, storageService) {
        super(id, group, telemetryService, themeService, storageService);
        this.inputDisposable = this._register(( (new MutableDisposable())));
    }
    createEditor(parent) {
        this.container = document.createElement('div');
        this.container.className = 'monaco-editor-pane-placeholder';
        this.container.style.outline = 'none';
        this.container.tabIndex = 0;
        this.scrollbar = this._register(( (new DomScrollableElement(
            this.container,
            { horizontal: ScrollbarVisibility.Auto, vertical: ScrollbarVisibility.Auto }
        ))));
        parent.appendChild(this.scrollbar.getDomNode());
    }
    async setInput(input, options, context, token) {
        await super.setInput(input, options, context, token);
        if (token.isCancellationRequested) {
            return;
        }
        this.inputDisposable.value = await this.renderInput(input, options);
    }
    async renderInput(input, options) {
        const [container, scrollbar] = assertAllDefined(this.container, this.scrollbar);
        clearNode(container);
        const disposables = ( (new DisposableStore()));
        const { icon, label, actions } = await this.getContents(input, options, disposables);
        const truncatedLabel = truncate(label, EditorPlaceholder_1.PLACEHOLDER_LABEL_MAX_LENGTH);
        const iconContainer = container.appendChild($('.editor-placeholder-icon-container'));
        const iconWidget = disposables.add(( (new SimpleIconLabel(iconContainer))));
        iconWidget.text = icon;
        const labelContainer = container.appendChild($('.editor-placeholder-label-container'));
        const labelWidget = document.createElement('span');
        labelWidget.textContent = truncatedLabel;
        labelContainer.appendChild(labelWidget);
        container.setAttribute('aria-label', `${computeEditorAriaLabel(input, undefined, this.group, undefined)}, ${truncatedLabel}`);
        if (actions.length) {
            const actionsContainer = container.appendChild($('.editor-placeholder-buttons-container'));
            const buttons = disposables.add(( (new ButtonBar(actionsContainer))));
            for (let i = 0; i < actions.length; i++) {
                const button = disposables.add(buttons.addButton({
                    ...defaultButtonStyles,
                    secondary: i !== 0
                }));
                button.label = actions[i].label;
                disposables.add(button.onDidClick(e => {
                    if (e) {
                        EventHelper.stop(e, true);
                    }
                    actions[i].run();
                }));
            }
        }
        scrollbar.scanDomNode();
        return disposables;
    }
    clearInput() {
        if (this.container) {
            clearNode(this.container);
        }
        this.inputDisposable.clear();
        super.clearInput();
    }
    layout(dimension) {
        const [container, scrollbar] = assertAllDefined(this.container, this.scrollbar);
        size(container, dimension.width, dimension.height);
        scrollbar.scanDomNode();
        container.classList.toggle('max-height-200px', dimension.height <= 200);
    }
    focus() {
        super.focus();
        this.container?.focus();
    }
    dispose() {
        this.container?.remove();
        super.dispose();
    }
};
EditorPlaceholder = EditorPlaceholder_1 = ( (__decorate([
    ( (__param(2, ITelemetryService))),
    ( (__param(3, IThemeService))),
    ( (__param(4, IStorageService)))
], EditorPlaceholder)));
let WorkspaceTrustRequiredPlaceholderEditor = class WorkspaceTrustRequiredPlaceholderEditor extends EditorPlaceholder {
    static { WorkspaceTrustRequiredPlaceholderEditor_1 = this; }
    static { this.ID = 'workbench.editors.workspaceTrustRequiredEditor'; }
    static { this.LABEL = ( localizeWithPath(_moduleId, 0, "Workspace Trust Required")); }
    static { this.DESCRIPTOR = EditorPaneDescriptor.create(WorkspaceTrustRequiredPlaceholderEditor_1, WorkspaceTrustRequiredPlaceholderEditor_1.ID, WorkspaceTrustRequiredPlaceholderEditor_1.LABEL); }
    constructor(group, telemetryService, themeService, commandService, workspaceService, storageService) {
        super(WorkspaceTrustRequiredPlaceholderEditor_1.ID, group, telemetryService, themeService, storageService);
        this.commandService = commandService;
        this.workspaceService = workspaceService;
    }
    getTitle() {
        return WorkspaceTrustRequiredPlaceholderEditor_1.LABEL;
    }
    async getContents() {
        return {
            icon: '$(workspace-untrusted)',
            label: isSingleFolderWorkspaceIdentifier(toWorkspaceIdentifier(this.workspaceService.getWorkspace())) ?
                ( localizeWithPath(
                    _moduleId,
                    1,
                    "The file is not displayed in the editor because trust has not been granted to the folder."
                )) :
                ( localizeWithPath(
                    _moduleId,
                    2,
                    "The file is not displayed in the editor because trust has not been granted to the workspace."
                )),
            actions: [
                {
                    label: ( localizeWithPath(_moduleId, 3, "Manage Workspace Trust")),
                    run: () => this.commandService.executeCommand('workbench.trust.manage')
                }
            ]
        };
    }
};
WorkspaceTrustRequiredPlaceholderEditor = WorkspaceTrustRequiredPlaceholderEditor_1 = ( (__decorate([
    ( (__param(1, ITelemetryService))),
    ( (__param(2, IThemeService))),
    ( (__param(3, ICommandService))),
    ( (__param(4, IWorkspaceContextService))),
    ( (__param(5, IStorageService)))
], WorkspaceTrustRequiredPlaceholderEditor)));
let ErrorPlaceholderEditor = class ErrorPlaceholderEditor extends EditorPlaceholder {
    static { ErrorPlaceholderEditor_1 = this; }
    static { this.ID = 'workbench.editors.errorEditor'; }
    static { this.LABEL = ( localizeWithPath(_moduleId, 4, "Error Editor")); }
    static { this.DESCRIPTOR = EditorPaneDescriptor.create(ErrorPlaceholderEditor_1, ErrorPlaceholderEditor_1.ID, ErrorPlaceholderEditor_1.LABEL); }
    constructor(group, telemetryService, themeService, storageService, fileService, dialogService) {
        super(ErrorPlaceholderEditor_1.ID, group, telemetryService, themeService, storageService);
        this.fileService = fileService;
        this.dialogService = dialogService;
    }
    async getContents(input, options, disposables) {
        const resource = input.resource;
        const error = options.error;
        const isFileNotFound = error?.fileOperationResult === FileOperationResult.FILE_NOT_FOUND;
        let label;
        if (isFileNotFound) {
            label = ( localizeWithPath(
                _moduleId,
                5,
                "The editor could not be opened because the file was not found."
            ));
        }
        else if (isEditorOpenError(error) && error.forceMessage) {
            label = error.message;
        }
        else if (error) {
            label = ( localizeWithPath(
                _moduleId,
                6,
                "The editor could not be opened due to an unexpected error: {0}",
                truncateMiddle(toErrorMessage(error), EditorPlaceholder.PLACEHOLDER_LABEL_MAX_LENGTH / 2)
            ));
        }
        else {
            label = ( localizeWithPath(_moduleId, 7, "The editor could not be opened due to an unexpected error."));
        }
        let icon = '$(error)';
        if (isEditorOpenError(error)) {
            if (error.forceSeverity === Severity$1.Info) {
                icon = '$(info)';
            }
            else if (error.forceSeverity === Severity$1.Warning) {
                icon = '$(warning)';
            }
        }
        let actions = undefined;
        if (isEditorOpenError(error) && error.actions.length > 0) {
            actions = ( (error.actions.map(action => {
                return {
                    label: action.label,
                    run: () => {
                        const result = action.run();
                        if (result instanceof Promise) {
                            result.catch(error => this.dialogService.error(toErrorMessage(error)));
                        }
                    }
                };
            })));
        }
        else {
            actions = [
                {
                    label: ( localizeWithPath(_moduleId, 8, "Try Again")),
                    run: () => this.group.openEditor(input, { ...options, source: EditorOpenSource.USER  })
                }
            ];
        }
        if (isFileNotFound && resource && this.fileService.hasProvider(resource)) {
            disposables.add(this.fileService.onDidFilesChange(e => {
                if (e.contains(resource, FileChangeType.ADDED, FileChangeType.UPDATED)) {
                    this.group.openEditor(input, options);
                }
            }));
        }
        return { icon, label, actions: actions ?? [] };
    }
};
ErrorPlaceholderEditor = ErrorPlaceholderEditor_1 = ( (__decorate([
    ( (__param(1, ITelemetryService))),
    ( (__param(2, IThemeService))),
    ( (__param(3, IStorageService))),
    ( (__param(4, IFileService))),
    ( (__param(5, IDialogService)))
], ErrorPlaceholderEditor)));
export { EditorPlaceholder, ErrorPlaceholderEditor, WorkspaceTrustRequiredPlaceholderEditor };
