"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.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 StagePanelLocation_1 = require("./StagePanelLocation");
const StagePanelSection_1 = require("./StagePanelSection");
const InternalFrontstageManager_1 = require("../frontstage/InternalFrontstageManager");
const UiFramework_1 = require("../UiFramework");
/** 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._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) {
        var _a, _b;
        this._location = location;
        this._initialConfig = config;
        this._start.initializeFromConfig((_a = config === null || config === void 0 ? void 0 : config.sections) === null || _a === void 0 ? void 0 : _a.start);
        this._end.initializeFromConfig((_b = config === null || config === void 0 ? void 0 : config.sections) === null || _b === void 0 ? void 0 : _b.end);
    }
    /** @internal */
    get initialConfig() {
        return this._initialConfig;
    }
    /** Current size of the panel */
    get size() {
        const frontstageDef = UiFramework_1.UiFramework.frontstages.activeFrontstageDef;
        const state = frontstageDef === null || frontstageDef === void 0 ? void 0 : frontstageDef.nineZoneState;
        if (!state)
            return this.defaultSize;
        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,
        });
    }
    /** @internal */
    handleSizeChanged(size) {
        InternalFrontstageManager_1.InternalFrontstageManager.onPanelSizeChangedEvent.emit({
            panelDef: this,
            size,
        });
    }
    /** Indicates whether the panel is resizable. */
    get resizable() {
        var _a, _b;
        return (_b = (_a = this._initialConfig) === null || _a === void 0 ? void 0 : _a.resizable) !== null && _b !== void 0 ? _b : true;
    }
    /** Indicates whether the panel is pinned. */
    get pinned() {
        var _a, _b;
        const frontstageDef = UiFramework_1.UiFramework.frontstages.activeFrontstageDef;
        const state = frontstageDef === null || frontstageDef === void 0 ? void 0 : frontstageDef.nineZoneState;
        if (!state)
            return (_b = (_a = this.initialConfig) === null || _a === void 0 ? void 0 : _a.pinned) !== null && _b !== void 0 ? _b : true;
        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 === null || frontstageDef === void 0 ? void 0 : 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.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() {
        var _a;
        const defaultState = (_a = this._initialConfig) === null || _a === void 0 ? void 0 : _a.defaultState;
        return defaultState !== null && defaultState !== void 0 ? defaultState : StagePanelState.Open;
    }
    /** @internal */
    get defaultSize() {
        var _a;
        return (_a = this._initialConfig) === null || _a === void 0 ? void 0 : _a.size;
    }
    /** 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 === null || config === void 0 ? void 0 : 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.Minimized : StagePanelState.Open;
}
exports.getPanelState = getPanelState;
//# sourceMappingURL=StagePanelDef.js.map