/*---------------------------------------------------------------------------------------------
 * 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
 */
import * as React from "react";
import { Dialog } from "@itwin/core-react";
import { DialogButtonType } from "@itwin/appui-abstract";
import { getUniqueId } from "@itwin/appui-layout-react";
import { DefaultDialogGridContainer } from "../uiprovider/DefaultDialogGridContainer";
import { ComponentGenerator } from "../uiprovider/ComponentGenerator";
import { UiFramework } from "../UiFramework";
/** Component to show dialog populated from properties supplied via uiDataProvider
 * @public
 */
export function UiDataProvidedDialog({ uiDataProvider, id, isModal, ...dialogProps }) {
    const dialogId = React.useRef(id ? id : getUniqueId());
    const dialogIsModal = React.useRef(isModal);
    const onOK = React.useRef();
    const onCancel = React.useRef();
    const closeDialog = () => {
        if (dialogIsModal.current)
            UiFramework.dialogs.modal.close();
        else
            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 (DialogButtonType.Cancel === button.type) {
                    onCancel.current = button.onClick;
                    button.onClick = handleCancel;
                    continue;
                }
                if (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(Dialog, { ...dialogProps, opened: true, modal: isModal, buttonCluster: buttonCluster, onClose: handleClose, onEscape: handleClose },
        React.createElement(DefaultDialogGridContainer, { componentGenerator: new ComponentGenerator(uiDataProvider) })));
}
//# sourceMappingURL=UiDataProvidedDialog.js.map