"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 Base
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.insertPanelWidget = exports.addPanelWidget = exports.getPanelSize = exports.getPanelPixelSizeFromSpec = exports.updatePanelState = exports.createPanelsState = exports.createHorizontalPanelState = exports.createVerticalPanelState = void 0;
const appui_abstract_1 = require("@itwin/appui-abstract");
const immer_1 = require("immer");
const Panel_1 = require("../../widget-panels/Panel");
const NineZoneStateHelpers_1 = require("./NineZoneStateHelpers");
const WidgetStateHelpers_1 = require("./WidgetStateHelpers");
function createPanelState(side) {
    return {
        collapseOffset: 100,
        collapsed: false,
        maxSize: 600,
        minSize: 200,
        pinned: true,
        resizable: true,
        side,
        size: undefined,
        widgets: [],
        maxWidgetCount: 2,
        splitterPercent: 50,
    };
}
/** @internal */
function createVerticalPanelState(side, args) {
    return {
        ...createPanelState(side),
        ...args,
        side,
    };
}
exports.createVerticalPanelState = createVerticalPanelState;
/** @internal */
function createHorizontalPanelState(side, args) {
    return {
        ...createPanelState(side),
        minSize: 100,
        span: true,
        ...args,
        side,
    };
}
exports.createHorizontalPanelState = createHorizontalPanelState;
/** @internal */
function createPanelsState(args) {
    return {
        bottom: createHorizontalPanelState("bottom"),
        left: createVerticalPanelState("left"),
        right: createVerticalPanelState("right"),
        top: createHorizontalPanelState("top"),
        ...args,
    };
}
exports.createPanelsState = createPanelsState;
/** @internal */
function updatePanelState(state, side, update) {
    return (0, immer_1.default)(state, (draft) => {
        const panel = draft.panels[side];
        update(panel);
    });
}
exports.updatePanelState = updatePanelState;
/** @internal */
function getPanelPixelSizeFromSpec(side, appSize, panelSize) {
    if (typeof panelSize === "number") {
        return panelSize;
    }
    const fullSize = (0, Panel_1.isHorizontalPanelSide)(side) ? appSize.height : appSize.width;
    return (panelSize.percentage / 100) * fullSize;
}
exports.getPanelPixelSizeFromSpec = getPanelPixelSizeFromSpec;
/** @internal */
function getPanelSize(preferredSizeSpec, side, minSizeSpec, maxSizeSpec, appSize) {
    if (preferredSizeSpec === undefined)
        return undefined;
    const maxSize = getPanelPixelSizeFromSpec(side, appSize, maxSizeSpec);
    const minSize = getPanelPixelSizeFromSpec(side, appSize, minSizeSpec);
    const preferredSize = getPanelPixelSizeFromSpec(side, appSize, preferredSizeSpec);
    return Math.min(Math.max(preferredSize, minSize), maxSize);
}
exports.getPanelSize = getPanelSize;
/** @internal */
function addPanelWidget(state, side, id, tabs, widgetArgs) {
    return insertPanelWidget(state, side, id, tabs, Infinity, widgetArgs);
}
exports.addPanelWidget = addPanelWidget;
/** @internal */
function insertPanelWidget(state, side, id, tabs, sectionIndex, widgetArgs) {
    const panel = state.panels[side];
    const maxWidgetCount = panel.maxWidgetCount;
    if (panel.widgets.length >= maxWidgetCount)
        // eslint-disable-next-line deprecation/deprecation
        throw new appui_abstract_1.UiError(NineZoneStateHelpers_1.category, "Max widget count exceeded", undefined, () => ({
            maxWidgetCount,
        }));
    state = (0, WidgetStateHelpers_1.addWidgetState)(state, id, tabs, widgetArgs);
    return (0, immer_1.default)(state, (draft) => {
        const widgets = draft.panels[side].widgets;
        widgets.splice(sectionIndex, 0, id);
    });
}
exports.insertPanelWidget = insertPanelWidget;
//# sourceMappingURL=PanelStateHelpers.js.map