var __defProp = Object.defineProperty;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
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;
};
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import React, { useEffect, useCallback } from "react";
import { createPortal } from "react-dom";
import FocusLock from "react-focus-lock";
import styled, { css } from "styled-components";
import { func, bool, node, number, string } from "prop-types";
import { Close } from "@gympass/yoga-icons";
import { usePortal, useCombinedRefs } from "../../hooks";
import { Button, Card, Box } from "../..";
import { focusOnFirstProgrammaticFocusableElement } from "./utils";
const StyledDialog = styled(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 = styled.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 = styled(Button.Icon)`
  ${({ theme }) => {
  const background = theme.yoga.colors.elements.lineAndBorders;
  const containerSize = theme.yoga.spacing.xxlarge;
  const svgSize = theme.yoga.spacing.medium;
  return css`
      width: ${containerSize}px;
      height: ${containerSize}px;
      background-color: ${background};

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

      &:active {
        background-color: ${background};
      }
    `;
}}
`;
const Dialog = React.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 = useCombinedRefs(forwardedRef);
    const dialogElement = usePortal(dialogId != null ? dialogId : "dialog");
    const isCloseButtonVisible = onClose && !hideCloseButton;
    const lockProps = __spreadValues({
      role: "dialog",
      "aria-modal": true,
      onClose
    }, props);
    const closeDialog = useCallback(
      (e) => {
        if (dialogRef.current === e.target && isOpen && onClose) {
          onClose(e);
        }
        return true;
      },
      [onClose, isOpen]
    );
    const keyPress = useCallback(
      (e) => {
        if (e.key === "Escape" && isOpen && onClose) {
          onClose(e);
        }
        return true;
      },
      [onClose, isOpen]
    );
    useEffect(() => {
      document.addEventListener("keydown", keyPress);
      return () => document.removeEventListener("keydown", keyPress);
    }, [keyPress]);
    return isOpen ? createPortal(
      /* @__PURE__ */ jsx(
        Overlay,
        {
          onClick: closeDialog,
          onClose,
          ref: dialogRef,
          zIndex,
          children: /* @__PURE__ */ jsxs(
            FocusLock,
            {
              as: StyledDialog,
              lockProps,
              returnFocus: true,
              disabled: !isOpen,
              className,
              onActivation: focusOnFirstProgrammaticFocusableElement,
              children: [
                isCloseButtonVisible && /* @__PURE__ */ jsx(Box, { d: "flex", justifyContent: "flex-end", w: "100%", children: /* @__PURE__ */ jsx(
                  CloseButton,
                  {
                    "aria-label": closeLabel,
                    icon: Close,
                    inverted: true,
                    secondary: true,
                    onClick: onClose
                  }
                ) }),
                children
              ]
            }
          )
        }
      ),
      dialogElement
    ) : /* @__PURE__ */ jsx(Fragment, {});
  }
);
Dialog.propTypes = {
  /** Custom id to allow creating isolated portal nodes */
  dialogId: string,
  /** Control the dialog visibility. */
  isOpen: bool,
  /** Hide the close button when onClose prop is defined. */
  hideCloseButton: bool,
  /** Function to close the dialog. */
  onClose: func,
  closeLabel: string,
  className: string,
  zIndex: number,
  children: node.isRequired
};
Dialog.displayName = "Dialog";
var Dialog_default = Dialog;
export {
  StyledDialog,
  Dialog_default as default
};
