
import { registerCss } from '../../../../../../../css.js';
import { __decorate, __param } from '../../../../../../../external/tslib/tslib.es6.js';
import * as exceptionWidget from './media/exceptionWidget.css';
import { localize } from '../../../../nls.js';
import { $ as $$1, append, isAncestorOfActiveElement } from '../../../../base/browser/dom.js';
import { ZoneWidget } from '../../../../editor/contrib/zoneWidget/browser/zoneWidget.js';
import { EDITOR_CONTRIBUTION_ID } from '../common/debug.js';
import { RunOnceScheduler } from '../../../../base/common/async.js';
import { IThemeService } from '../../../../platform/theme/common/themeService.service.js';
import { ThemeIcon } from '../../../../base/common/themables.js';
import { registerColor } from '../../../../platform/theme/common/colorUtils.js';
import '../../../../platform/theme/common/colors/baseColors.js';
import '../../../../platform/theme/common/colors/chartsColors.js';
import '../../../../platform/theme/common/colors/editorColors.js';
import '../../../../platform/theme/common/colors/inputColors.js';
import '../../../../platform/theme/common/colors/listColors.js';
import '../../../../platform/theme/common/colors/menuColors.js';
import '../../../../platform/theme/common/colors/minimapColors.js';
import '../../../../platform/theme/common/colors/miscColors.js';
import '../../../../platform/theme/common/colors/quickpickColors.js';
import '../../../../platform/theme/common/colors/searchColors.js';
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
import { LinkDetector, DebugLinkHoverBehavior } from './linkDetector.js';
import { EditorOption } from '../../../../editor/common/config/editorOptions.js';
import { ActionBar } from '../../../../base/browser/ui/actionbar/actionbar.js';
import { Action } from '../../../../base/common/actions.js';
import { widgetClose } from '../../../../platform/theme/common/iconRegistry.js';

registerCss(exceptionWidget);
const $ = $$1;
const debugExceptionWidgetBorder = registerColor("debugExceptionWidget.border", "#a31515", ( localize(9246, "Exception widget border color.")));
const debugExceptionWidgetBackground = registerColor("debugExceptionWidget.background", {
    dark: "#420b0d",
    light: "#f1dfde",
    hcDark: "#420b0d",
    hcLight: "#f1dfde"
}, ( localize(9247, "Exception widget background color.")));
let ExceptionWidget = class ExceptionWidget extends ZoneWidget {
    constructor(
        editor,
        exceptionInfo,
        debugSession,
        shouldScroll,
        themeService,
        instantiationService
    ) {
        super(editor, {
            showFrame: true,
            showArrow: true,
            isAccessible: true,
            frameWidth: 1,
            className: "exception-widget-container"
        });
        this.exceptionInfo = exceptionInfo;
        this.debugSession = debugSession;
        this.shouldScroll = shouldScroll;
        this.instantiationService = instantiationService;
        this.applyTheme(themeService.getColorTheme());
        this._disposables.add(themeService.onDidColorThemeChange(this.applyTheme.bind(this)));
        this.create();
        const onDidLayoutChangeScheduler = ( new RunOnceScheduler(() => this._doLayout(undefined, undefined), 50));
        this._disposables.add(this.editor.onDidLayoutChange(() => onDidLayoutChangeScheduler.schedule()));
        this._disposables.add(onDidLayoutChangeScheduler);
    }
    applyTheme(theme) {
        this.backgroundColor = theme.getColor(debugExceptionWidgetBackground);
        const frameColor = theme.getColor(debugExceptionWidgetBorder);
        this.style({
            arrowColor: frameColor,
            frameColor: frameColor
        });
    }
    _applyStyles() {
        if (this.container) {
            this.container.style.backgroundColor = this.backgroundColor ? ( this.backgroundColor.toString()) : "";
        }
        super._applyStyles();
    }
    _fillContainer(container) {
        this.setCssClass("exception-widget");
        const fontInfo = this.editor.getOption(EditorOption.fontInfo);
        container.style.fontSize = `${fontInfo.fontSize}px`;
        container.style.lineHeight = `${fontInfo.lineHeight}px`;
        container.tabIndex = 0;
        const title = $(".title");
        const label = $(".label");
        append(title, label);
        const actions = $(".actions");
        append(title, actions);
        label.textContent = this.exceptionInfo.id ? ( localize(9248, "Exception has occurred: {0}", this.exceptionInfo.id)) : ( localize(9249, "Exception has occurred."));
        let ariaLabel = label.textContent;
        const actionBar = this._disposables.add(( new ActionBar(actions)));
        actionBar.push(( new Action("editor.closeExceptionWidget", ( localize(9250, "Close")), ThemeIcon.asClassName(widgetClose), true, async () => {
            const contribution = this.editor.getContribution(EDITOR_CONTRIBUTION_ID);
            contribution?.closeExceptionWidget();
        })), {
            label: false,
            icon: true
        });
        append(container, title);
        if (this.exceptionInfo.description) {
            const description = $(".description");
            description.textContent = this.exceptionInfo.description;
            ariaLabel += ", " + this.exceptionInfo.description;
            append(container, description);
        }
        if (this.exceptionInfo.details && this.exceptionInfo.details.stackTrace) {
            const stackTrace = $(".stack-trace");
            const linkDetector = this.instantiationService.createInstance(LinkDetector);
            const hoverBehaviour = {
                store: this._disposables,
                type: DebugLinkHoverBehavior.Rich
            };
            const linkedStackTrace = linkDetector.linkify(
                this.exceptionInfo.details.stackTrace,
                hoverBehaviour,
                true,
                this.debugSession ? this.debugSession.root : undefined,
                undefined
            );
            stackTrace.appendChild(linkedStackTrace);
            append(container, stackTrace);
            ariaLabel += ", " + this.exceptionInfo.details.stackTrace;
        }
        container.setAttribute("aria-label", ariaLabel);
    }
    _doLayout(_heightInPixel, _widthInPixel) {
        this.container.style.height = "initial";
        const lineHeight = this.editor.getOption(EditorOption.lineHeight);
        const arrowHeight = Math.round(lineHeight / 3);
        const computedLinesNumber = Math.ceil((this.container.offsetHeight + arrowHeight) / lineHeight);
        this._relayout(computedLinesNumber);
    }
    revealRange(range, isLastLine) {
        if (this.shouldScroll()) {
            super.revealRange(range, isLastLine);
        }
    }
    focus() {
        this.container?.focus();
    }
    hasFocus() {
        if (!this.container) {
            return false;
        }
        return isAncestorOfActiveElement(this.container);
    }
    getWhitespaceHeight() {
        if (!this._viewZone || !this._viewZone.id) {
            return 0;
        }
        const whitespaces = this.editor.getWhitespaces();
        const whitespace = whitespaces.find(ws => ws.id === this._viewZone.id);
        return whitespace ? whitespace.height : 0;
    }
};
ExceptionWidget = ( __decorate([( __param(4, IThemeService)), ( __param(5, IInstantiationService))], ExceptionWidget));

export { ExceptionWidget };
