import * as Common from "../../core/common/common.js";
import * as i18n from "../../core/i18n/i18n.js";
import * as SDK from "../../core/sdk/sdk.js";
import * as UI from "../../ui/legacy/legacy.js";
import {
    ScreencastView
} from "./ScreencastView.js";
const UIStrings = {
        toggleScreencast: "Toggle screencast"
    },
    str_ = i18n.i18n.registerUIStrings("panels/screencast/ScreencastApp.ts", UIStrings),
    i18nString = i18n.i18n.getLocalizedString.bind(void 0, str_);
let appInstance, toolbarButtonProviderInstance, screencastAppProviderInstance;
export class ScreencastApp {
    enabledSetting;
    toggleButton;
    rootSplitWidget;
    screenCaptureModel;
    screencastView;
    rootView;
    constructor() {
        this.enabledSetting = Common.Settings.Settings.instance().createSetting("screencastEnabled", !1), this.toggleButton = new UI.Toolbar.ToolbarToggle(i18nString(UIStrings.toggleScreencast), "devices"), this.toggleButton.setToggled(this.enabledSetting.get()), this.toggleButton.setEnabled(!1), this.toggleButton.addEventListener(UI.Toolbar.ToolbarButton.Events.Click, this.toggleButtonClicked, this), SDK.TargetManager.TargetManager.instance().observeModels(SDK.ScreenCaptureModel.ScreenCaptureModel, this)
    }
    static instance() {
        return appInstance || (appInstance = new ScreencastApp), appInstance
    }
    presentUI(e) {
        this.rootView = new UI.RootView.RootView, this.rootSplitWidget = new UI.SplitWidget.SplitWidget(!1, !0, "InspectorView.screencastSplitViewState", 300, 300), this.rootSplitWidget.setVertical(!0), this.rootSplitWidget.setSecondIsSidebar(!0), this.rootSplitWidget.show(this.rootView.element), this.rootSplitWidget.hideMain(), this.rootSplitWidget.setSidebarWidget(UI.InspectorView.InspectorView.instance()), UI.InspectorView.InspectorView.instance().setOwnerSplit(this.rootSplitWidget), this.rootView.attachToDocument(e), this.rootView.focus()
    }
    modelAdded(e) {
        e.target() === SDK.TargetManager.TargetManager.instance().primaryPageTarget() && (this.screenCaptureModel = e, this.toggleButton.setEnabled(!0), this.screencastView = new ScreencastView(e), this.rootSplitWidget && this.rootSplitWidget.setMainWidget(this.screencastView), this.screencastView.initialize(), this.onScreencastEnabledChanged())
    }
    modelRemoved(e) {
        this.screenCaptureModel === e && (delete this.screenCaptureModel, this.toggleButton.setEnabled(!1), this.screencastView && (this.screencastView.detach(), delete this.screencastView), this.onScreencastEnabledChanged())
    }
    toggleButtonClicked() {
        const e = !this.toggleButton.toggled();
        this.enabledSetting.set(e), this.onScreencastEnabledChanged()
    }
    onScreencastEnabledChanged() {
        if (!this.rootSplitWidget) return;
        const e = Boolean(this.enabledSetting.get() && this.screencastView);
        this.toggleButton.setToggled(e), e ? this.rootSplitWidget.showBoth() : this.rootSplitWidget.hideMain()
    }
}
export class ToolbarButtonProvider {
    static instance(e = {
        forceNew: !1
    }) {
        const {
            forceNew: t
        } = e;
        return toolbarButtonProviderInstance && !t || (toolbarButtonProviderInstance = new ToolbarButtonProvider), toolbarButtonProviderInstance
    }
    item() {
        return ScreencastApp.instance().toggleButton
    }
}
export class ScreencastAppProvider {
    static instance(e = {
        forceNew: !1
    }) {
        const {
            forceNew: t
        } = e;
        return screencastAppProviderInstance && !t || (screencastAppProviderInstance = new ScreencastAppProvider), screencastAppProviderInstance
    }
    createApp() {
        return ScreencastApp.instance()
    }
}
