"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 Dialog
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContentDialog = exports.useActiveContentControlId = void 0;
require("./ContentDialog.scss");
const React = require("react");
const core_react_1 = require("@itwin/core-react");
const SyncUiEventDispatcher_1 = require("../syncui/SyncUiEventDispatcher");
const UiFramework_1 = require("../UiFramework");
const classnames_1 = require("classnames");
// istanbul ignore next
/**
 *@internal
 */
function useActiveContentControlId() {
    var _a;
    const [activeContentId, setActiveContentId] = React.useState((_a = UiFramework_1.UiFramework.content.getActiveContentControl()) === null || _a === void 0 ? void 0 : _a.uniqueId);
    React.useEffect(() => {
        const onActiveContentChanged = (_args) => {
            var _a;
            setActiveContentId((_a = UiFramework_1.UiFramework.content.getActiveContentControl()) === null || _a === void 0 ? void 0 : _a.uniqueId);
        };
        // IModelApp.viewManager.onSelectedViewportChanged will often fire before UI components have mounted
        // so use UiFramework.content.onActiveContentChangedEvent which will always trigger once all stage components
        // are loaded and when the IModelApp.viewManager.selectedView changes.
        UiFramework_1.UiFramework.content.onActiveContentChangedEvent.addListener(onActiveContentChanged);
        return () => {
            UiFramework_1.UiFramework.content.onActiveContentChangedEvent.removeListener(onActiveContentChanged);
        };
    }, []);
    React.useEffect(() => {
        const syncIdsOfInterest = [SyncUiEventDispatcher_1.SyncUiEventId.ActiveContentChanged, SyncUiEventDispatcher_1.SyncUiEventId.ContentControlActivated, SyncUiEventDispatcher_1.SyncUiEventId.FrontstageReady];
        const handleSyncUiEvent = (args) => {
            var _a;
            // istanbul ignore else
            if (syncIdsOfInterest.some((value) => args.eventIds.has(value))) {
                setActiveContentId((_a = UiFramework_1.UiFramework.content.getActiveContentControl()) === null || _a === void 0 ? void 0 : _a.uniqueId);
            }
        };
        return SyncUiEventDispatcher_1.SyncUiEventDispatcher.onSyncUiEvent.addListener(handleSyncUiEvent);
    }, []);
    return activeContentId;
}
exports.useActiveContentControlId = useActiveContentControlId;
/** Content Dialog React component uses the Dialog component with a modal={false} prop.
 * It controls the z-index to keep the focused dialog above content but below widgets.
 * @public
 */
function ContentDialog(props) {
    const { className, children, dialogId, style, modal, modelessId, onModelessPointerDown, ...otherProps } = props; // eslint-disable-line @typescript-eslint/no-unused-vars
    const activeContentControlId = useActiveContentControlId();
    // istanbul ignore next
    const dialogClassName = React.useMemo(() => (0, classnames_1.default)(activeContentControlId === dialogId ? "active-content-dialog" : "inactive-content-dialog", className), [activeContentControlId, className, dialogId]);
    const [zIndex, setZIndex] = React.useState(UiFramework_1.UiFramework.content.dialogs.getZIndex(dialogId));
    const updateZIndex = React.useCallback(() => {
        const newZ = UiFramework_1.UiFramework.content.dialogs.getZIndex(dialogId);
        // istanbul ignore else
        if (newZ !== zIndex) {
            setZIndex(newZ);
        }
    }, [dialogId, zIndex]);
    return (React.createElement(core_react_1.Dialog, { className: dialogClassName, "data-item-type": "content-dialog", "data-item-id": dialogId, resizable: true, movable: true, trapFocus: false, modal: false, ...otherProps, modelessId: dialogId, onModelessPointerDown: (event) => UiFramework_1.UiFramework.content.dialogs.handlePointerDownEvent(event, dialogId, updateZIndex), style: { zIndex, ...style } }, children));
}
exports.ContentDialog = ContentDialog;
//# sourceMappingURL=ContentDialog.js.map