"use strict";
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/* eslint-disable deprecation/deprecation */
/** @packageDocumentation
 * @module Frontstage
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.setPanelSize = exports.toPanelSide = exports.StagePanelZoneDef = exports.StagePanelZonesDef = exports.StagePanelDef = exports.PanelSizeChangedEvent = exports.PanelStateChangedEvent = exports.StagePanelState = void 0;
const immer_1 = require("immer");
const appui_abstract_1 = require("@itwin/appui-abstract");
const WidgetDef_1 = require("../widgets/WidgetDef");
const WidgetHost_1 = require("../widgets/WidgetHost");
const Zone_1 = require("../zones/Zone");
const UiFramework_1 = require("../UiFramework");
const InternalFrontstageManager_1 = require("../frontstage/InternalFrontstageManager");
/** Enum for StagePanel state.
 * @public
 */
var StagePanelState;
(function (StagePanelState) {
    StagePanelState[StagePanelState["Off"] = 0] = "Off";
    StagePanelState[StagePanelState["Minimized"] = 1] = "Minimized";
    StagePanelState[StagePanelState["Open"] = 2] = "Open";
    StagePanelState[StagePanelState["Popup"] = 3] = "Popup";
})(StagePanelState = exports.StagePanelState || (exports.StagePanelState = {}));
/** Panel State Changed Event class.
 * @beta
 */
class PanelStateChangedEvent extends appui_abstract_1.UiEvent {
}
exports.PanelStateChangedEvent = PanelStateChangedEvent;
/** @internal */
class PanelSizeChangedEvent extends appui_abstract_1.UiEvent {
}
exports.PanelSizeChangedEvent = PanelSizeChangedEvent;
/**
 * A StagePanelDef represents each Stage Panel within a Frontstage.
 * @public
 */
class StagePanelDef extends WidgetHost_1.WidgetHost {
    /** Constructor for PanelDef.
     */
    constructor() {
        super();
        this._panelState = StagePanelState.Open;
        this._defaultState = StagePanelState.Open;
        this._resizable = true;
        this._pinned = true;
        this._location = appui_abstract_1.StagePanelLocation.Left;
        this._panelZones = new StagePanelZonesDef();
    }
    /** @internal */
    get maxSizeSpec() { return this._maxSizeSpec; }
    /** @internal */
    get minSize() { return this._minSize; }
    /** Default size of the panel */
    get size() {
        // istanbul ignore next
        if ("1" === UiFramework_1.UiFramework.uiVersion)
            return this._size;
        // istanbul ignore else
        if (UiFramework_1.UiFramework.frontstages.activeFrontstageDef) {
            const [_, size] = UiFramework_1.UiFramework.frontstages.activeFrontstageDef.getPanelCurrentState(this);
            return size;
        }
        // istanbul ignore next
        return this._defaultSize;
    }
    set size(size) {
        if (this._size === size)
            return;
        // istanbul ignore else
        if (UiFramework_1.UiFramework.uiVersion === "2") {
            const frontstageDef = UiFramework_1.UiFramework.frontstages.activeFrontstageDef;
            // istanbul ignore else
            if (frontstageDef && frontstageDef.nineZoneState) {
                const side = toPanelSide(this.location);
                frontstageDef.nineZoneState = (0, exports.setPanelSize)(frontstageDef.nineZoneState, side, size);
                const panel = frontstageDef.nineZoneState.panels[side];
                if (panel.size === this._size)
                    return;
                size = panel.size;
            }
        }
        this._size = size;
        InternalFrontstageManager_1.InternalFrontstageManager.onPanelSizeChangedEvent.emit({
            panelDef: this,
            size,
        });
    }
    /** Indicates whether the panel is resizable. */
    // istanbul ignore next
    get resizable() { return this._resizable; }
    /** Indicates whether the panel is pinned. */
    get pinned() { return this._pinned; }
    /** Any application data to attach to this Panel. */
    get applicationData() { return this._applicationData; }
    /** Location of panel. */
    get location() { return this._location; }
    /** Panel state. Defaults to PanelState.Open. */
    get panelState() {
        var _a;
        if ("1" === UiFramework_1.UiFramework.uiVersion)
            return this._panelState;
        // istanbul ignore else
        if (UiFramework_1.UiFramework.frontstages.activeFrontstageDef) {
            const [state] = (_a = UiFramework_1.UiFramework.frontstages.activeFrontstageDef) === null || _a === void 0 ? void 0 : _a.getPanelCurrentState(this);
            return state;
        }
        // istanbul ignore next
        return this.defaultState;
    }
    set panelState(panelState) {
        if (panelState === this._panelState)
            return;
        const frontstageDef = UiFramework_1.UiFramework.frontstages.activeFrontstageDef;
        if (UiFramework_1.UiFramework.uiVersion === "2" && frontstageDef && frontstageDef.nineZoneState) {
            const side = toPanelSide(this.location);
            frontstageDef.nineZoneState = (0, immer_1.default)(frontstageDef.nineZoneState, (nineZone) => {
                const panel = nineZone.panels[side];
                switch (panelState) {
                    case StagePanelState.Minimized: {
                        panel.collapsed = true;
                        break;
                    }
                    case StagePanelState.Open: {
                        panel.collapsed = false;
                        break;
                    }
                    case StagePanelState.Off: {
                        panel.collapsed = true;
                        break;
                    }
                }
            });
        }
        this._panelState = panelState;
        UiFramework_1.UiFramework.frontstages.onPanelStateChangedEvent.emit({
            panelDef: this,
            panelState,
        });
    }
    /** @internal */
    get defaultState() { return this._defaultState; }
    /** @internal */
    get defaultSize() { return this._defaultSize; }
    /** Panel zones.
     * @internal
     */
    get panelZones() {
        return this._panelZones;
    }
    /** @internal */
    initializeFromProps(props, location) {
        if (location !== undefined)
            this._location = location;
        if (!props)
            return;
        this._size = props.size;
        this._defaultSize = props.size;
        this._maxSizeSpec = props.maxSize;
        this._minSize = props.minSize;
        if (props.defaultState !== undefined) {
            this._panelState = props.defaultState;
            this._defaultState = props.defaultState;
        }
        this._resizable = props.resizable;
        if (props.pinned !== undefined)
            this._pinned = props.pinned;
        this._applicationData = props.applicationData;
        if (props.panelZones) {
            this._panelZones.initializeFromProps(props.panelZones, this._location);
        }
        if (props.widgets) {
            props.widgets.forEach((widgetNode, index) => {
                const stableId = `uifw-sp-${appui_abstract_1.StagePanelLocation[this._location]}-${index}`;
                const stableProps = (0, Zone_1.getStableWidgetProps)(widgetNode.props, stableId);
                const widgetDef = new WidgetDef_1.WidgetDef(stableProps);
                this.addWidgetDef(widgetDef);
            });
        }
    }
    /** Gets the list of Widgets. */
    get widgetDefs() {
        const widgetDefs = [...super.widgetDefs];
        for (const [, panelZone] of this.panelZones) {
            widgetDefs.push(...panelZone.widgetDefs);
        }
        return widgetDefs;
    }
    /** Gets the list of Widgets (w/o StagePanelZone widgets).
     * @internal
     */
    get panelWidgetDefs() {
        return super.widgetDefs;
    }
    /** @internal */
    updateDynamicWidgetDefs(stageId, stageUsage, location, _section, allStageWidgetDefs, frontstageApplicationData) {
        this.panelZones.start.updateDynamicWidgetDefs(stageId, stageUsage, location, appui_abstract_1.StagePanelSection.Start, allStageWidgetDefs, frontstageApplicationData);
        this.panelZones.end.updateDynamicWidgetDefs(stageId, stageUsage, location, appui_abstract_1.StagePanelSection.Middle, allStageWidgetDefs, frontstageApplicationData);
        this.panelZones.end.updateDynamicWidgetDefs(stageId, stageUsage, location, appui_abstract_1.StagePanelSection.End, allStageWidgetDefs, frontstageApplicationData);
    }
}
exports.StagePanelDef = StagePanelDef;
const stagePanelZoneDefKeys = ["start", "end"];
/** @internal */
class StagePanelZonesDef {
    constructor() {
        this._start = new StagePanelZoneDef();
        this._end = new StagePanelZoneDef();
    }
    get start() {
        return this._start;
    }
    get end() {
        return this._end;
    }
    /** @internal */
    initializeFromProps(props, panelLocation) {
        if (props.start) {
            this.start.initializeFromProps(props.start, panelLocation, "start");
        }
        if (props.middle) {
            this.end.initializeFromProps(props.middle, panelLocation, "end");
        }
        if (props.end) {
            this.end.initializeFromProps(props.end, panelLocation, "end");
        }
    }
    /** @internal */
    *[Symbol.iterator]() {
        for (const key of stagePanelZoneDefKeys) {
            const value = this[key];
            yield [key, value];
        }
        return undefined;
    }
}
exports.StagePanelZonesDef = StagePanelZonesDef;
/** @internal */
class StagePanelZoneDef extends WidgetHost_1.WidgetHost {
    /** @internal */
    initializeFromProps(props, panelLocation, panelZone) {
        props.widgets.forEach((widgetNode, index) => {
            const stableId = `uifw-spz-${appui_abstract_1.StagePanelLocation[panelLocation]}-${panelZone}-${index}`;
            const stableProps = (0, Zone_1.getStableWidgetProps)(widgetNode.props, stableId);
            const widgetDef = new WidgetDef_1.WidgetDef(stableProps);
            this.addWidgetDef(widgetDef);
        });
    }
}
exports.StagePanelZoneDef = StagePanelZoneDef;
/** @internal */
function toPanelSide(location) {
    switch (location) {
        case appui_abstract_1.StagePanelLocation.Bottom:
        case appui_abstract_1.StagePanelLocation.BottomMost:
            return "bottom";
        case appui_abstract_1.StagePanelLocation.Left:
            return "left";
        case appui_abstract_1.StagePanelLocation.Right:
            return "right";
        case appui_abstract_1.StagePanelLocation.Top:
        case appui_abstract_1.StagePanelLocation.TopMost:
            return "top";
    }
}
exports.toPanelSide = toPanelSide;
/** @internal */
exports.setPanelSize = (0, immer_1.default)((nineZone, side, size) => {
    const panel = nineZone.panels[side];
    panel.size = size === undefined ? size : Math.min(Math.max(size, panel.minSize), panel.maxSize);
});
//# sourceMappingURL=StagePanelDef.js.map