"use strict";
/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module Frontstage
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPanelState = exports.setPanelPinned = exports.toPanelSide = exports.StagePanelSectionDef = exports.StagePanelDef = exports.PanelStateChangedEvent = void 0;
const appui_abstract_1 = require("@itwin/appui-abstract");
const immer_1 = require("immer");
const UiFramework_1 = require("../UiFramework");
const InternalFrontstageManager_1 = require("../frontstage/InternalFrontstageManager");
const WidgetDef_1 = require("../widgets/WidgetDef");
const WidgetHost_1 = require("../widgets/WidgetHost");
const StagePanelLocation_1 = require("./StagePanelLocation");
const StagePanelSection_1 = require("./StagePanelSection");
const StagePanelState_1 = require("./StagePanelState");
/** Panel state changed event class.
 * @beta
 * @deprecated in 4.13.0. This class should not be used by applications to instantiate objects.
 */
// eslint-disable-next-line deprecation/deprecation
class PanelStateChangedEvent extends appui_abstract_1.UiEvent {
}
exports.PanelStateChangedEvent = PanelStateChangedEvent;
/**
 * A StagePanelDef represents each Stage Panel within a Frontstage.
 * @public
 */
class StagePanelDef extends WidgetHost_1.WidgetHost {
    /** Constructor for PanelDef. */
    constructor() {
        super();
        this._location = StagePanelLocation_1.StagePanelLocation.Left;
        this._start = new StagePanelSectionDef();
        this._end = new StagePanelSectionDef();
    }
    /** @internal */
    static create(config, location) {
        const def = new StagePanelDef();
        def.initializeFromConfig(config, location);
        return def;
    }
    /** @internal */
    initializeFromConfig(config, location) {
        this._location = location;
        this._initialConfig = config;
        this._start.initializeFromConfig(config?.sections?.start);
        this._end.initializeFromConfig(config?.sections?.end);
    }
    /** @internal */
    get initialConfig() {
        return this._initialConfig;
    }
    /** Current size of the panel.
     * @deprecated in 4.12.0. Use {@link StagePanelDef.sizeSpec} instead.
     */
    get size() {
        const frontstageDef = UiFramework_1.UiFramework.frontstages.activeFrontstageDef;
        const state = frontstageDef?.nineZoneState;
        // eslint-disable-next-line deprecation/deprecation
        if (!state)
            return this.initialConfig?.size;
        const side = toPanelSide(this.location);
        const panel = state.panels[side];
        return panel.size;
    }
    set size(size) {
        const frontstageDef = UiFramework_1.UiFramework.frontstages.activeFrontstageDef;
        if (!frontstageDef)
            return;
        const side = toPanelSide(this.location);
        frontstageDef.dispatch({
            type: "PANEL_SET_SIZE",
            side,
            size,
        });
    }
    /** Current size of the panel */
    get sizeSpec() {
        const frontstageDef = UiFramework_1.UiFramework.frontstages.activeFrontstageDef;
        const state = frontstageDef?.nineZoneState;
        if (!state)
            return this.initialConfig?.sizeSpec;
        const side = toPanelSide(this.location);
        const panel = state.panels[side];
        return panel.size;
    }
    set sizeSpec(size) {
        const frontstageDef = UiFramework_1.UiFramework.frontstages.activeFrontstageDef;
        if (!frontstageDef)
            return;
        const side = toPanelSide(this.location);
        frontstageDef.dispatch({
            type: "PANEL_SET_SIZE",
            side,
            size,
        });
    }
    /** @internal */
    handleSizeChanged(size) {
        InternalFrontstageManager_1.InternalFrontstageManager.onPanelSizeChangedEvent.emit({
            panelDef: this,
            size,
        });
    }
    /** Indicates whether the panel is resizable. */
    get resizable() {
        return this.defaultResizable;
    }
    /** Indicates whether the panel is pinned. */
    get pinned() {
        const frontstageDef = UiFramework_1.UiFramework.frontstages.activeFrontstageDef;
        const state = frontstageDef?.nineZoneState;
        if (!state)
            return this.defaultPinned;
        const side = toPanelSide(this.location);
        const panel = state.panels[side];
        return panel.pinned;
    }
    set pinned(pinned) {
        const frontstageDef = UiFramework_1.UiFramework.frontstages.activeFrontstageDef;
        if (!frontstageDef)
            return;
        const side = toPanelSide(this.location);
        frontstageDef.dispatch({
            type: "PANEL_SET_PINNED",
            side,
            pinned,
        });
    }
    /** @internal */
    handlePinnedChanged(pinned) {
        UiFramework_1.UiFramework.frontstages.onPanelPinnedChangedEvent.emit({
            panelDef: this,
            pinned,
        });
    }
    /** Location of panel. */
    get location() {
        return this._location;
    }
    /** Panel state. Defaults to PanelState.Open. */
    get panelState() {
        const frontstageDef = UiFramework_1.UiFramework.frontstages.activeFrontstageDef;
        const state = frontstageDef?.nineZoneState;
        if (!state)
            return this.defaultState;
        const side = toPanelSide(this.location);
        return getPanelState(state, side);
    }
    set panelState(panelState) {
        const frontstageDef = UiFramework_1.UiFramework.frontstages.activeFrontstageDef;
        if (!frontstageDef)
            return;
        const side = toPanelSide(this.location);
        const collapsed = panelState === StagePanelState_1.StagePanelState.Open ? false : true;
        frontstageDef.dispatch({
            type: "PANEL_SET_COLLAPSED",
            collapsed,
            side,
        });
    }
    /** @internal */
    handlePanelStateChanged(panelState) {
        UiFramework_1.UiFramework.frontstages.onPanelStateChangedEvent.emit({
            panelDef: this,
            panelState,
        });
    }
    /** @internal */
    get defaultState() {
        const defaultState = this._initialConfig?.defaultState;
        return defaultState ?? StagePanelState_1.StagePanelState.Open;
    }
    /** @internal */
    get defaultPinned() {
        return this._initialConfig?.pinned ?? true;
    }
    /** @internal */
    get defaultResizable() {
        return this._initialConfig?.resizable ?? true;
    }
    /** Gets the list of Widgets. */
    get widgetDefs() {
        return [
            ...super.widgetDefs,
            ...this._start.widgetDefs,
            ...this._end.widgetDefs,
        ];
    }
    /** @internal */
    updateDynamicWidgetDefs(stageId, stageUsage, location, _section, allStageWidgetDefs) {
        this._start.updateDynamicWidgetDefs(stageId, stageUsage, location, StagePanelSection_1.StagePanelSection.Start, allStageWidgetDefs);
        this._end.updateDynamicWidgetDefs(stageId, stageUsage, location, StagePanelSection_1.StagePanelSection.End, allStageWidgetDefs);
    }
    /** @internal */
    getPanelSectionDef(section) {
        switch (section) {
            case StagePanelSection_1.StagePanelSection.Start: {
                return this._start;
            }
            case StagePanelSection_1.StagePanelSection.End: {
                return this._end;
            }
        }
    }
}
exports.StagePanelDef = StagePanelDef;
/** @internal */
class StagePanelSectionDef extends WidgetHost_1.WidgetHost {
    /** @internal */
    initializeFromConfig(config) {
        config?.forEach((widgetConfig) => {
            const widgetDef = WidgetDef_1.WidgetDef.create(widgetConfig);
            this.addWidgetDef(widgetDef);
        });
    }
}
exports.StagePanelSectionDef = StagePanelSectionDef;
/** @internal */
function toPanelSide(location) {
    switch (location) {
        case StagePanelLocation_1.StagePanelLocation.Bottom:
            return "bottom";
        case StagePanelLocation_1.StagePanelLocation.Left:
            return "left";
        case StagePanelLocation_1.StagePanelLocation.Right:
            return "right";
        case StagePanelLocation_1.StagePanelLocation.Top:
            return "top";
    }
}
exports.toPanelSide = toPanelSide;
/** @internal */
exports.setPanelPinned = (0, immer_1.default)((nineZone, side, pinned) => {
    const panel = nineZone.panels[side];
    panel.pinned = pinned;
});
/** @internal */
function getPanelState(state, side) {
    const panel = state.panels[side];
    return panel.collapsed ? StagePanelState_1.StagePanelState.Minimized : StagePanelState_1.StagePanelState.Open;
}
exports.getPanelState = getPanelState;
//# sourceMappingURL=StagePanelDef.js.map