
import { __decorate, __param } from '../../../../../../../external/tslib/tslib.es6.js';
import { Codicon } from '../../../../base/common/codicons.js';
import { truncate } from '../../../../base/common/strings.js';
import { URI } from '../../../../base/common/uri.js';
import { generateUuid } from '../../../../base/common/uuid.js';
import { BrowserViewUri } from '../../../../platform/browserView/common/browserViewUri.js';
import { BrowserViewSharingState } from './browserView.js';
import { IBrowserViewWorkbenchService } from './browserView.service.js';
import { Verbosity, EditorInputCapabilities } from '../../../common/editor.js';
import { EditorInput } from '../../../common/editor/editorInput.js';
import { IThemeService } from '../../../../platform/theme/common/themeService.service.js';
import { TAB_ACTIVE_FOREGROUND } from '../../../common/theme.js';
import { localize } from '../../../../nls.js';
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
import { hasKey } from '../../../../base/common/types.js';
import { ITelemetryService } from '../../../../platform/telemetry/common/telemetry.service.js';
import { logBrowserOpen } from '../../../../platform/browserView/common/browserViewTelemetry.js';
import { LRUCachedFunction } from '../../../../base/common/cache.js';
import { DisposableStore, Disposable } from '../../../../base/common/lifecycle.js';
import { Emitter, Event } from '../../../../base/common/event.js';

var BrowserEditorInput_1;
const LOADING_SPINNER_SVG = color => `
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16">
        <path d="M8 1a7 7 0 1 0 0 14 7 7 0 0 0 0-14zm0 1.5a5.5 5.5 0 1 1 0 11 5.5 5.5 0 0 1 0-11z" fill="${color}" opacity="0.3"/>
        <path d="M8 1a7 7 0 0 1 7 7h-1.5A5.5 5.5 0 0 0 8 2.5V1z" fill="${color}">
            <animateTransform attributeName="transform" type="rotate" dur="1s" repeatCount="indefinite" values="0 8 8;360 8 8"/>
        </path>
    </svg>
`;
const MAX_TITLE_LENGTH = 30;
let BrowserEditorInput = class BrowserEditorInput extends EditorInput {
    static {
        BrowserEditorInput_1 = this;
    }
    static {
        this.ID = "workbench.editorinputs.browser";
    }
    static {
        this.EDITOR_ID = "workbench.editor.browser";
    }
    static {
        this.DEFAULT_LABEL = ( localize(4753, "Browser"));
    }
    constructor(
        options,
        _resolveModel,
        themeService,
        instantiationService,
        telemetryService,
        browserViewWorkbenchService
    ) {
        super();
        this._resolveModel = _resolveModel;
        this.themeService = themeService;
        this.instantiationService = instantiationService;
        this.telemetryService = telemetryService;
        this.browserViewWorkbenchService = browserViewWorkbenchService;
        this._modelStore = this._register(( new DisposableStore()));
        this._onBeforeDispose = this._register(( new Emitter()));
        this.onBeforeDispose = this._onBeforeDispose.event;
        this._onDidResolveModel = this._register(( new Emitter()));
        this.onDidResolveModel = this._onDidResolveModel.event;
        this.getURLTitles = ( new LRUCachedFunction(url => {
            let _parsed = undefined;
            let _short = undefined;
            let _medium = undefined;
            let _long = undefined;
            function getParsed() {
                if (!_parsed) {
                    _parsed = ( URI.parse(url));
                }
                return _parsed;
            }
            return {
                get [Verbosity.SHORT]() {
                    if (!_short) {
                        _short = getParsed().authority;
                    }
                    return _short;
                },
                get [Verbosity.MEDIUM]() {
                    if (!_medium) {
                        _medium = ( getParsed().with({
                            query: "",
                            fragment: ""
                        }).toString());
                    }
                    return _medium;
                },
                get [Verbosity.LONG]() {
                    if (!_long) {
                        _long = ( getParsed().with({
                            fragment: ""
                        }).toString());
                    }
                    return _long;
                }
            };
        }));
        this._id = options.id;
        this._initialData = options;
    }
    get model() {
        return this._model;
    }
    set model(model) {
        if (this._model === model) {
            return;
        }
        this._modelStore.clear();
        this._model = model;
        this._modelStore.add(this._model.onWillDispose(() => {
            this._modelStore.clear();
            this._model = undefined;
        }));
        this._modelStore.add(this._model.onDidClose(() => {
            this.dispose(true);
        }));
        this._modelStore.add(this._model.onDidChangeTitle(() => this._onDidChangeLabel.fire()));
        this._modelStore.add(this._model.onDidChangeFavicon(() => this._onDidChangeLabel.fire()));
        this._modelStore.add(this._model.onDidChangeLoadingState(() => this._onDidChangeLabel.fire()));
        this._modelStore.add(this._model.onDidNavigate(() => this._onDidChangeLabel.fire()));
        this._onDidChangeLabel.fire();
        this._onDidResolveModel.fire(model);
    }
    onceModelResolves(cb) {
        if (this._model) {
            cb(this._model);
            return Disposable.None;
        } else {
            return Event.once(this.onDidResolveModel)(cb);
        }
    }
    get id() {
        return this._id;
    }
    get url() {
        return this._model ? this._model.url : this._initialData.url;
    }
    get title() {
        return this._model ? this._model.title : this._initialData.title;
    }
    get favicon() {
        return this._model ? this._model.favicon : this._initialData.favicon;
    }
    get isDefaultLinkOpen() {
        return !!this._initialData.isDefaultLinkOpen;
    }
    get isSharingAvailable() {
        return this._model ? this._model.sharingState !== BrowserViewSharingState.Unavailable : this.browserViewWorkbenchService.isSharingAvailable;
    }
    navigate(url) {
        if (this._model) {
            void this._model.loadURL(url);
        } else {
            this._initialData = {
                id: this._id,
                url
            };
            this._onDidChangeLabel.fire();
        }
    }
    async resolve() {
        if (!this._model && !this._modelPromise) {
            this._modelPromise = (async () => {
                this._model = await this._resolveModel();
                this._modelPromise = undefined;
                return this._model;
            })();
        }
        return this._model || this._modelPromise;
    }
    get typeId() {
        return BrowserEditorInput_1.ID;
    }
    get editorId() {
        return BrowserEditorInput_1.EDITOR_ID;
    }
    get capabilities() {
        return EditorInputCapabilities.ForceReveal | EditorInputCapabilities.Readonly;
    }
    get resource() {
        return BrowserViewUri.forId(this._id);
    }
    getIcon() {
        if (this._model) {
            if (this._model.loading) {
                const color = this.themeService.getColorTheme().getColor(TAB_ACTIVE_FOREGROUND);
                return ( URI.parse(
                    "data:image/svg+xml;utf8," + encodeURIComponent(LOADING_SPINNER_SVG(color?.toString()))
                ));
            }
            if (this._model.favicon) {
                return ( URI.parse(this._model.favicon));
            }
            return Codicon.globe;
        }
        if (this._initialData.favicon) {
            return ( URI.parse(this._initialData.favicon));
        }
        return Codicon.globe;
    }
    getName() {
        const hasTitle = this._model ? !!this._model.title : !!this._initialData.title;
        const name = hasTitle ? this.title : this.getDescription(Verbosity.SHORT) || BrowserEditorInput_1.DEFAULT_LABEL;
        return truncate(name, MAX_TITLE_LENGTH);
    }
    getTitle(verbosity = Verbosity.MEDIUM) {
        const hasTitle = this._model ? !!this._model.title : !!this._initialData.title;
        const description = this.getDescription(verbosity);
        const title = hasTitle ? `${this.title} (${description})` : description;
        return title || BrowserEditorInput_1.DEFAULT_LABEL;
    }
    getDescription(verbosity = Verbosity.MEDIUM) {
        return this.url && this.getURLTitles.get(this.url)[verbosity];
    }
    canReopen() {
        return true;
    }
    matches(otherInput) {
        if (super.matches(otherInput)) {
            return true;
        }
        if (otherInput instanceof BrowserEditorInput_1) {
            return this._id === otherInput._id;
        }
        if (hasKey(otherInput, {
            resource: true
        }) && otherInput.resource?.scheme === BrowserViewUri.scheme) {
            const parsed = BrowserViewUri.parse(otherInput.resource);
            if (parsed) {
                return this._id === parsed.id;
            }
        }
        return false;
    }
    copy() {
        logBrowserOpen(this.telemetryService, "copyToNewWindow");
        return this.instantiationService.invokeFunction(accessor => {
            const browserViewWorkbenchService = accessor.get(IBrowserViewWorkbenchService);
            return browserViewWorkbenchService.getOrCreateLazy(generateUuid(), {
                url: this.url,
                title: this.title,
                favicon: this.favicon
            });
        });
    }
    toUntyped() {
        const viewState = {
            url: this.url,
            title: this.title,
            favicon: this.favicon
        };
        return {
            resource: this.resource,
            options: {
                override: BrowserEditorInput_1.EDITOR_ID,
                viewState
            }
        };
    }
    dispose(force) {
        if (!force) {
            let vetoed = false;
            this._onBeforeDispose.fire({
                veto: () => {
                    vetoed = true;
                }
            });
            if (vetoed) {
                return;
            }
        }
        super.dispose();
        if (this._model) {
            this._initialData = {
                id: this._id,
                url: this._model.url,
                title: this._model.title,
                favicon: this._model.favicon
            };
            this._model.dispose();
            this._model = undefined;
        }
    }
    serialize() {
        return {
            id: this._id,
            url: this.url,
            title: this.title,
            favicon: this.favicon
        };
    }
};
BrowserEditorInput = BrowserEditorInput_1 = ( __decorate([( __param(2, IThemeService)), ( __param(3, IInstantiationService)), ( __param(4, ITelemetryService)), ( __param(5, IBrowserViewWorkbenchService))], BrowserEditorInput));

export { BrowserEditorInput };
