"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __objRest = (source, exclude) => {
  var target = {};
  for (var prop in source)
    if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
      target[prop] = source[prop];
  if (source != null && __getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(source)) {
      if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
        target[prop] = source[prop];
    }
  return target;
};
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var Dialog_exports = {};
__export(Dialog_exports, {
  StyledDialog: () => StyledDialog,
  default: () => Dialog_default
});
module.exports = __toCommonJS(Dialog_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_react = __toESM(require("react"));
var import_react_dom = require("react-dom");
var import_react_focus_lock = __toESM(require("react-focus-lock"));
var import_styled_components = __toESM(require("styled-components"));
var import_prop_types = require("prop-types");
var import_yoga_icons = require("@gympass/yoga-icons");
var import_hooks = require("../../hooks");
var import__ = require("../..");
var import_utils = require("./utils");
const StyledDialog = (0, import_styled_components.default)(import__.Card)`
  ${({
  onClose,
  theme: {
    yoga: {
      components: { dialog }
    }
  }
}) => `
  padding: ${onClose ? dialog.padding.withCloseButton : dialog.padding.top}px
  ${dialog.padding.default}px
  ${dialog.padding.default}px;

  width: ${dialog.width.default}px;
  min-height: ${dialog.height.min}px;
  border-radius: ${dialog.border.radius}px;
  `}
  display: flex;
  align-items: center;
  flex-direction: column;
`;
const Overlay = import_styled_components.default.div`
  ${({
  zIndex,
  theme: {
    yoga: {
      components: { dialog }
    }
  }
}) => `
  display: flex;
  z-index: ${zIndex};
  justify-content: center;
  align-items: center;

  position: fixed;
  top: ${dialog.position.default};
  right: ${dialog.position.default};
  left: ${dialog.position.default};
  bottom: ${dialog.position.default};

  background-color: rgba(35, 27, 34, 0.48);
  `}
`;
const CloseButton = (0, import_styled_components.default)(import__.Button.Icon)`
  ${({ theme }) => {
  const background = theme.yoga.colors.elements.lineAndBorders;
  const containerSize = theme.yoga.spacing.xxlarge;
  const svgSize = theme.yoga.spacing.medium;
  return import_styled_components.css`
      width: ${containerSize}px;
      height: ${containerSize}px;
      background-color: ${background};

      svg {
        width: ${svgSize}px;
        height: ${svgSize}px;
      }

      &:active {
        background-color: ${background};
      }
    `;
}}
`;
const Dialog = import_react.default.forwardRef(
  (_a, forwardedRef) => {
    var _b = _a, {
      isOpen = false,
      hideCloseButton = false,
      children,
      dialogId,
      onClose,
      zIndex = 3,
      closeLabel,
      className
    } = _b, props = __objRest(_b, [
      "isOpen",
      "hideCloseButton",
      "children",
      "dialogId",
      "onClose",
      "zIndex",
      "closeLabel",
      "className"
    ]);
    const dialogRef = (0, import_hooks.useCombinedRefs)(forwardedRef);
    const dialogElement = (0, import_hooks.usePortal)(dialogId != null ? dialogId : "dialog");
    const isCloseButtonVisible = onClose && !hideCloseButton;
    const lockProps = __spreadValues({
      role: "dialog",
      "aria-modal": true,
      onClose
    }, props);
    const closeDialog = (0, import_react.useCallback)(
      (e) => {
        if (dialogRef.current === e.target && isOpen && onClose) {
          onClose(e);
        }
        return true;
      },
      [onClose, isOpen]
    );
    const keyPress = (0, import_react.useCallback)(
      (e) => {
        if (e.key === "Escape" && isOpen && onClose) {
          onClose(e);
        }
        return true;
      },
      [onClose, isOpen]
    );
    (0, import_react.useEffect)(() => {
      document.addEventListener("keydown", keyPress);
      return () => document.removeEventListener("keydown", keyPress);
    }, [keyPress]);
    return isOpen ? (0, import_react_dom.createPortal)(
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
        Overlay,
        {
          onClick: closeDialog,
          onClose,
          ref: dialogRef,
          zIndex,
          children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
            import_react_focus_lock.default,
            {
              as: StyledDialog,
              lockProps,
              returnFocus: true,
              disabled: !isOpen,
              className,
              onActivation: import_utils.focusOnFirstProgrammaticFocusableElement,
              children: [
                isCloseButtonVisible && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import__.Box, { d: "flex", justifyContent: "flex-end", w: "100%", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
                  CloseButton,
                  {
                    "aria-label": closeLabel,
                    icon: import_yoga_icons.Close,
                    inverted: true,
                    secondary: true,
                    onClick: onClose
                  }
                ) }),
                children
              ]
            }
          )
        }
      ),
      dialogElement
    ) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, {});
  }
);
Dialog.propTypes = {
  /** Custom id to allow creating isolated portal nodes */
  dialogId: import_prop_types.string,
  /** Control the dialog visibility. */
  isOpen: import_prop_types.bool,
  /** Hide the close button when onClose prop is defined. */
  hideCloseButton: import_prop_types.bool,
  /** Function to close the dialog. */
  onClose: import_prop_types.func,
  closeLabel: import_prop_types.string,
  className: import_prop_types.string,
  zIndex: import_prop_types.number,
  children: import_prop_types.node.isRequired
};
Dialog.displayName = "Dialog";
var Dialog_default = Dialog;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  StyledDialog
});
