"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const RadixDialog = require("@radix-ui/react-dialog");
const React = require("react");
const ReactDOM = require("react-dom");
const useCompareValues = require("../../hooks/useCompareValues.cjs");
const classNames = require("../../utils/classNames.cjs");
const config_context = require("../config/config.context.cjs");
require("./dialog.theme.css.cjs");
function _interopNamespaceDefault(e) {
  const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
  if (e) {
    for (const k in e) {
      if (k !== "default") {
        const d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(n, k, d.get ? d : {
          enumerable: true,
          get: () => e[k]
        });
      }
    }
  }
  n.default = e;
  return Object.freeze(n);
}
const RadixDialog__namespace = /* @__PURE__ */ _interopNamespaceDefault(RadixDialog);
const React__namespace = /* @__PURE__ */ _interopNamespaceDefault(React);
const ReactDOM__namespace = /* @__PURE__ */ _interopNamespaceDefault(ReactDOM);
const Dialog = React__namespace.forwardRef(
  (props, ref) => {
    const { children, title, description, closeButtonIcon, open, onOpenChange, onClose, className, data, overlayClassName, testId, delayCloseFor, ...nativeProps } = props;
    const globals = config_context.useArmstrongConfig({
      dialogCloseButtonIcon: closeButtonIcon
    });
    const [finishAction, setFinishAction] = React__namespace.useState(open ? void 0 : "close");
    const [visible, setVisible] = React__namespace.useState(open);
    const resolverRef = React__namespace.useRef();
    const finishActionChanged = useCompareValues.useCompareValues(finishAction);
    const openHasChanged = useCompareValues.useCompareValues(open);
    const openChangeListeners = React__namespace.useRef([]);
    const onInnerOpenChange = React__namespace.useCallback((val) => {
      setFinishAction(val ? void 0 : "close");
      if (!val && delayCloseFor) {
        setTimeout(() => {
          setVisible(false);
        }, delayCloseFor);
        return;
      }
      setVisible(val);
    }, [delayCloseFor]);
    const onOpen = React__namespace.useCallback(() => new Promise((res) => {
      onInnerOpenChange(true);
      resolverRef.current = res;
    }), [onInnerOpenChange]);
    const setOk = React__namespace.useCallback(() => setFinishAction("ok"), []);
    const setClose = React__namespace.useCallback(() => setFinishAction("close"), []);
    const setCancel = React__namespace.useCallback(() => setFinishAction("cancel"), []);
    React__namespace.useImperativeHandle(ref, () => ({
      open: onOpen,
      close: setClose,
      ok: setOk,
      cancel: setCancel,
      isOpen: !!visible,
      addOpenChangeListener: (listener) => {
        openChangeListeners.current.push(listener);
        return () => {
          openChangeListeners.current = openChangeListeners.current.filter((l) => l !== listener);
        };
      }
    }), [onOpen, setCancel, setClose, setOk, visible]);
    React__namespace.useEffect(() => {
      if (finishAction && resolverRef.current) {
        resolverRef.current({ action: finishAction, data });
        resolverRef.current = void 0;
      }
      if (finishActionChanged) {
        onOpenChange == null ? void 0 : onOpenChange(!finishAction);
        if (finishAction) {
          onClose == null ? void 0 : onClose();
          if (delayCloseFor) {
            setTimeout(() => {
              setVisible(false);
            }, delayCloseFor);
          } else {
            setVisible(false);
          }
        }
      }
    }, [finishActionChanged, finishAction, data, onOpenChange, onClose, delayCloseFor]);
    React__namespace.useEffect(() => {
      if (openHasChanged && open !== void 0) {
        onInnerOpenChange(open);
      }
    }, [open, onInnerOpenChange, openHasChanged]);
    React__namespace.useEffect(() => {
      openChangeListeners.current.forEach((listener) => listener(!!visible));
    }, [visible]);
    return jsxRuntime.jsx(RadixDialog__namespace.Root, { open: visible, onOpenChange: onInnerOpenChange, children: globals.globalPortalTo && ReactDOM__namespace.createPortal(jsxRuntime.jsx(RadixDialog__namespace.Overlay, { className: classNames.concat("arm-dialog-overlay", overlayClassName), "data-visible": !finishAction, children: jsxRuntime.jsxs(RadixDialog__namespace.Content, { className: classNames.concat("arm-dialog", className), "data-has-title": title ? true : void 0, "data-testid": testId, "data-visible": !finishAction, ...nativeProps, children: [title && jsxRuntime.jsx(RadixDialog__namespace.Title, { className: "arm-dialog-title", children: title }), description && jsxRuntime.jsx(RadixDialog__namespace.Description, { className: "arm-dialog-description", children: description }), children && jsxRuntime.jsx("div", { className: "arm-dialog-content", children }), globals.dialogCloseButtonIcon !== false && jsxRuntime.jsx(RadixDialog__namespace.Close, { className: "arm-dialog-close", "aria-label": "Close", children: globals.dialogCloseButtonIcon })] }) }), globals.globalPortalTo) });
  }
  // type assertion to ensure generic works with RefForwarded component
  // DO NOT CHANGE TYPE WITHOUT CHANGING THIS, FIND TYPE BY INSPECTING React.forwardRef
);
Dialog.displayName = "Dialog";
exports.Dialog = Dialog;
//# sourceMappingURL=dialog.component.cjs.map
