"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.UiDataProvidedDialog = void 0;
const React = require("react");
const core_react_1 = require("@itwin/core-react");
const appui_abstract_1 = require("@itwin/appui-abstract");
const appui_layout_react_1 = require("@itwin/appui-layout-react");
const DefaultDialogGridContainer_1 = require("../uiprovider/DefaultDialogGridContainer");
const ComponentGenerator_1 = require("../uiprovider/ComponentGenerator");
const UiFramework_1 = require("../UiFramework");
/** Component to show dialog populated from properties supplied via uiDataProvider
 * @public
 */
function UiDataProvidedDialog({ uiDataProvider, id, isModal, ...dialogProps }) {
    const dialogId = React.useRef(id ? id : (0, appui_layout_react_1.getUniqueId)());
    const dialogIsModal = React.useRef(isModal);
    const onOK = React.useRef();
    const onCancel = React.useRef();
    const closeDialog = () => {
        if (dialogIsModal.current)
            UiFramework_1.UiFramework.dialogs.modal.close();
        else
            UiFramework_1.UiFramework.dialogs.modeless.close(dialogId.current);
    };
    const handleOk = React.useCallback(() => {
        onOK.current && onOK.current();
        closeDialog();
    }, []);
    const handleCancel = React.useCallback(() => {
        onCancel.current && onCancel.current();
        closeDialog();
    }, []);
    const generateButtonCluster = React.useCallback((buttons) => {
        // istanbul ignore else
        if (buttons) {
            for (const button of buttons) {
                if (appui_abstract_1.DialogButtonType.Cancel === button.type) {
                    onCancel.current = button.onClick;
                    button.onClick = handleCancel;
                    continue;
                }
                if (appui_abstract_1.DialogButtonType.OK === button.type) {
                    onOK.current = button.onClick;
                    button.onClick = handleOk;
                    continue;
                }
            }
        }
        return buttons;
    }, [handleCancel, handleOk]);
    const [buttonCluster, setButtonCluster] = React.useState(() => generateButtonCluster(uiDataProvider.supplyButtonData()));
    React.useEffect(() => {
        const handleReloaded = () => {
            setButtonCluster(generateButtonCluster(uiDataProvider.supplyButtonData()));
        };
        uiDataProvider.onItemsReloadedEvent.addListener(handleReloaded);
        return () => {
            uiDataProvider.onItemsReloadedEvent.removeListener(handleReloaded);
        };
    }, [generateButtonCluster, uiDataProvider]);
    React.useEffect(() => {
        const handleButtonReloaded = () => {
            setButtonCluster(generateButtonCluster(uiDataProvider.supplyButtonData()));
        };
        uiDataProvider.onButtonsReloadedEvent.addListener(handleButtonReloaded);
        return () => {
            uiDataProvider.onButtonsReloadedEvent.removeListener(handleButtonReloaded);
        };
    }, [generateButtonCluster, uiDataProvider]);
    const handleClose = React.useCallback(() => closeDialog(), []);
    return (React.createElement(core_react_1.Dialog, { ...dialogProps, opened: true, modal: isModal, buttonCluster: buttonCluster, onClose: handleClose, onEscape: handleClose },
        React.createElement(DefaultDialogGridContainer_1.DefaultDialogGridContainer, { componentGenerator: new ComponentGenerator_1.ComponentGenerator(uiDataProvider) })));
}
exports.UiDataProvidedDialog = UiDataProvidedDialog;
//# sourceMappingURL=UiDataProvidedDialog.js.map