"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.useLabel = exports.getUniqueId = exports.handleToCursorType = exports.sideToCursorType = exports.UiIsVisibleContext = exports.MeasureContext = exports.FloatingWidgetNodeContext = exports.TabNodeContext = exports.ToolSettingsNodeContext = exports.AnimateDockedToolSettingsContext = exports.AutoCollapseUnpinnedPanelsContext = exports.ShowWidgetIconContext = exports.WidgetContentNodeContext = exports.CursorTypeContext = exports.NineZoneLabelsContext = exports.NineZoneDispatchContext = exports.NineZoneProvider = exports.NineZone = void 0;
const React = require("react");
const core_bentley_1 = require("@itwin/core-bentley");
const core_react_1 = require("@itwin/core-react");
const ContentManager_1 = require("../widget/ContentManager");
const FloatingWidget_1 = require("../widget/FloatingWidget");
const DragManager_1 = require("./DragManager");
const Tab_1 = require("../widget/Tab");
const LayoutStore_1 = require("./LayoutStore");
/** @internal */
function NineZone(props) {
    const { children, ...providerProps } = props;
    const measurerRef = React.useRef(null);
    const measure = React.useCallback(() => {
        (0, core_bentley_1.assert)(!!measurerRef.current);
        return core_react_1.Rectangle.create(measurerRef.current.getBoundingClientRect());
    }, []);
    return (React.createElement(NineZoneProvider, { measure: measure, ...providerProps },
        React.createElement(Measurer, { ref: measurerRef }),
        children));
}
exports.NineZone = NineZone;
const tab = React.createElement(Tab_1.WidgetTab, null);
const floatingWidget = React.createElement(FloatingWidget_1.FloatingWidget, null);
/** @internal */
function NineZoneProvider(props) {
    return (React.createElement(LayoutStore_1.LayoutStoreContext.Provider, { value: props.layout },
        React.createElement(exports.NineZoneDispatchContext.Provider, { value: props.dispatch },
            React.createElement(exports.NineZoneLabelsContext.Provider, { value: props.labels },
                React.createElement(exports.UiIsVisibleContext.Provider, { value: !!props.uiIsVisible },
                    React.createElement(exports.ShowWidgetIconContext.Provider, { value: !!props.showWidgetIcon },
                        React.createElement(exports.AutoCollapseUnpinnedPanelsContext.Provider, { value: !!props.autoCollapseUnpinnedPanels },
                            React.createElement(exports.WidgetContentNodeContext.Provider, { value: props.widgetContent },
                                React.createElement(exports.ToolSettingsNodeContext.Provider, { value: props.toolSettingsContent },
                                    React.createElement(exports.TabNodeContext.Provider, { value: props.tab || tab },
                                        React.createElement(exports.FloatingWidgetNodeContext.Provider, { value: props.floatingWidget || floatingWidget },
                                            React.createElement(exports.AnimateDockedToolSettingsContext.Provider, { value: !!props.animateDockedToolSettings },
                                                React.createElement(DragManager_1.DragProvider, null,
                                                    React.createElement(CursorTypeProvider, null,
                                                        React.createElement(ContentManager_1.WidgetContentManager, null,
                                                            React.createElement(exports.MeasureContext.Provider, { value: props.measure }, props.children))))))))))))))));
}
exports.NineZoneProvider = NineZoneProvider;
/** @internal */
exports.NineZoneDispatchContext = React.createContext(null);
exports.NineZoneDispatchContext.displayName = "nz:NineZoneDispatchContext";
/** @internal */
exports.NineZoneLabelsContext = React.createContext(undefined);
exports.NineZoneLabelsContext.displayName = "nz:NineZoneLabelsContext";
/** @internal */
exports.CursorTypeContext = React.createContext(undefined);
exports.CursorTypeContext.displayName = "nz:CursorTypeContext";
/** @internal */
exports.WidgetContentNodeContext = React.createContext(undefined);
exports.WidgetContentNodeContext.displayName = "nz:WidgetContentNodeContext";
/** @internal */
exports.ShowWidgetIconContext = React.createContext(false);
exports.ShowWidgetIconContext.displayName = "nz:ShowWidgetIconContext";
/** @internal */
exports.AutoCollapseUnpinnedPanelsContext = React.createContext(false);
exports.AutoCollapseUnpinnedPanelsContext.displayName =
    "nz:AutoCollapseUnpinnedPanelsContext";
/** @internal */
exports.AnimateDockedToolSettingsContext = React.createContext(false);
exports.AnimateDockedToolSettingsContext.displayName =
    "nz:AnimateDockedToolSettingsContext";
/** @internal */
exports.ToolSettingsNodeContext = React.createContext(undefined);
exports.ToolSettingsNodeContext.displayName = "nz:ToolSettingsNodeContext";
/** @internal */
exports.TabNodeContext = React.createContext(undefined);
exports.TabNodeContext.displayName = "nz:TabNodeContext";
/** @internal */
exports.FloatingWidgetNodeContext = React.createContext(undefined);
exports.FloatingWidgetNodeContext.displayName = "nz:FloatingWidgetNodeContext";
/** @internal */
exports.MeasureContext = React.createContext(null);
exports.MeasureContext.displayName = "nz:MeasureContext";
/** @internal */
exports.UiIsVisibleContext = React.createContext(false);
exports.UiIsVisibleContext.displayName = "nz:UiIsVisibleContext";
function CursorTypeProvider(props) {
    const draggedTab = (0, LayoutStore_1.useLayout)((state) => !!state.draggedTab);
    const draggedWidgetId = React.useContext(DragManager_1.DraggedWidgetIdContext);
    const draggedPanelSide = React.useContext(DragManager_1.DraggedPanelSideContext);
    const draggedResizeHandle = React.useContext(DragManager_1.DraggedResizeHandleContext);
    let type;
    if (draggedTab || draggedWidgetId)
        type = "grabbing";
    else if (draggedPanelSide)
        type = sideToCursorType(draggedPanelSide);
    else if (draggedResizeHandle)
        type = handleToCursorType(draggedResizeHandle);
    return (React.createElement(exports.CursorTypeContext.Provider, { value: type }, props.children));
}
const Measurer = React.forwardRef(function Measurer(_, ref) {
    const size = React.useRef({});
    const dispatch = React.useContext(exports.NineZoneDispatchContext);
    const handleResize = React.useCallback((width, height) => {
        if (size.current.width === width && size.current.height === height)
            return;
        size.current.height = height;
        size.current.width = width;
        dispatch({
            type: "RESIZE",
            size: {
                height,
                width,
            },
        });
    }, [dispatch]);
    const roRef = (0, core_react_1.useResizeObserver)(handleResize);
    const refs = (0, core_react_1.useRefs)(ref, roRef);
    return (React.createElement("div", { ref: refs, style: {
            position: "absolute",
            width: "100%",
            height: "100%",
            top: "0",
            left: "0",
            visibility: "hidden",
        } }));
});
/** @internal */
function sideToCursorType(side) {
    switch (side) {
        case "bottom":
        case "top":
            return "ns-resize";
        case "left":
        case "right":
            return "ew-resize";
    }
}
exports.sideToCursorType = sideToCursorType;
/** @internal */
function handleToCursorType(handle) {
    switch (handle) {
        case "bottom":
        case "top":
            return "ns-resize";
        case "left":
        case "right":
            return "ew-resize";
        case "topLeft":
        case "bottomRight":
            return "nwse-resize";
        case "topRight":
        case "bottomLeft":
            return "nesw-resize";
    }
}
exports.handleToCursorType = handleToCursorType;
/** @internal */
function getUniqueId() {
    return core_bentley_1.Guid.createValue();
}
exports.getUniqueId = getUniqueId;
/** @internal */
function useLabel(labelKey) {
    const labels = React.useContext(exports.NineZoneLabelsContext);
    return labels?.[labelKey];
}
exports.useLabel = useLabel;
//# sourceMappingURL=NineZone.js.map