'use strict';

var React = require('react');
var core = require('@mantine/core');
var utils = require('./utils-b92a8b1c.js');
var react = require('@emotion/react');
var styled = require('@emotion/styled');
var miscTheme = require('./miscTheme.js');
var animation = require('./animation-f2389c7b.js');
var index = require('./index-a31ed3e1.js');

function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefault(React);
var styled__default = /*#__PURE__*/_interopDefault(styled);

const DURATION = '600ms';
const generateStyleBasedOnPlacement = (placement, open) => {
    switch (placement) {
        case 'top':
            return react.css `
        top: 0;
        width: 100%;
        border-radius: 0px 0px 6px 6px;
        ${open
                ? react.css `
              animation: ${animation.slideInDown} ${DURATION} ease-in-out forwards;
            `
                : null}

        &.slideOut {
          animation: ${animation.slideOutUp} ${DURATION} ease-in-out forwards;
        }
      `;
        case 'right':
            return react.css `
        right: 0;
        height: 100%;
        min-width: 300px;
        border-radius: 6px 0px 0px 6px;

        ${open
                ? react.css `
              animation: ${animation.slideInRight} ${DURATION} ease-in-out forwards;
            `
                : null}

        &.slideOut {
          animation: ${animation.slideOutRight} ${DURATION} ease-in-out forwards;
        }
      `;
        case 'bottom':
            return react.css `
        bottom: 0;
        width: 100%;
        border-radius: 6px 6px 0px 0px;
        ${open
                ? react.css `
              animation: ${animation.slideInUp} ${DURATION} ease-in-out forwards;
            `
                : null}

        &.slideOut {
          animation: ${animation.slideOutDown} ${DURATION} ease-in-out forwards;
        }
      `;
        case 'left':
            return react.css `
        left: 0;
        height: 100%;
        min-width: 300px;
        border-radius: 0px 6px 6px 0px;

        ${open
                ? react.css `
              animation: ${animation.slideInLeft} ${DURATION} ease-in-out forwards;
            `
                : null}

        &.slideOut {
          animation: ${animation.slideOutLeft} ${DURATION} ease-in-out forwards;
        }
      `;
        default:
            return react.css `
        left: 0;
        height: 100%;
        min-width: 300px;
        border-radius: 0px 6px 6px 0px;

        ${open
                ? react.css `
              animation: ${animation.slideInUp} ${DURATION} ease-in-out forwards;
            `
                : null}

        &.slideOut {
          animation: ${animation.slideOutDown} ${DURATION} ease-in-out forwards;
        }
      `;
    }
};

const StyledDrawer = styled__default["default"].div `
  position: fixed;
  inset: 0;
  z-index: ${miscTheme.theme.zIndexes.drawer};
  pointer-events: ${(props) => (props.open ? 'auto' : 'none')};
`;
const StyledDrawerPanel = styled__default["default"].div `
  position: fixed;
  z-index: 300;
  background: #ffffff;
  visibility: ${(props) => (props.open ? 'visible' : 'hidden')};
  max-height: calc(100vh - 100px);
  display: flex;
  flex-direction: column;
  .title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px;
    ${(props) => props.hasTitle
    ? react.css `
            border-bottom: 1px solid ${miscTheme.theme.colors.neutral100};
          `
    : null}
  }

  .close-button {
    margin-left: auto;
  }

  ${(props) => generateStyleBasedOnPlacement(props.placement, props.open)}
`;
const StyledDrawerContent = styled__default["default"].div `
  min-height: 200px;
  padding: 16px;
  overflow-y: auto;
`;

const StyledBackdrop = styled__default["default"].div `
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  overflow-x: hidden;
  overflow-y: auto;
  outline: 0;
  background-color: rgba(38, 38, 38, 0.3);
  z-index: 1;
  &[data-open='false'] {
    visibility: hidden;
    pointer-events: none;
  }
  &[data-open='true'] {
    visibility: visible;
    pointer-events: auto;
  }
`;

const Backdrop = ({ open = false, onClickOutside, }) => {
    return (React__default["default"].createElement(StyledBackdrop, { "data-open": open, onClick: onClickOutside }));
};

/**
 * @deprecated Consider to use at '@hhgtech/hhg-components/mantine'
 */
const Drawer = ({ children, title, onClose, open = false, placement = 'left', destroyOnClose = false, isPropagationEvent = true, className, }) => {
    const ref = React.useRef();
    React.useEffect(() => {
        utils.toggleNoScroll(open);
        return () => utils.toggleNoScroll(false);
    }, [open]);
    const onCloseDrawer = () => {
        ref.current.classList.add('slideOut');
        setTimeout(() => {
            onClose();
        }, 800);
    };
    if (!open && destroyOnClose) {
        return null;
    }
    return (React__default["default"].createElement(core.Portal, null,
        React__default["default"].createElement(StyledDrawer, { className: className, open: open },
            React__default["default"].createElement(Backdrop, { open: open, onClickOutside: onCloseDrawer }),
            React__default["default"].createElement(StyledDrawerPanel, { open: open, placement: placement, ref: ref, hasTitle: Boolean(title) },
                React__default["default"].createElement("div", { className: "title" },
                    title && (React__default["default"].createElement(index.Text, { size: "s2", weight: "semiBold" }, title)),
                    React__default["default"].createElement("svg", { width: "24", height: "24", viewBox: "0 0 24 24", fill: "none", className: "close-button", onClick: onCloseDrawer },
                        React__default["default"].createElement("path", { d: "M5.99007 4.99067C5.79114 4.99072 5.59674 5.0501 5.43173 5.16121C5.26672 5.27233 5.1386 5.43013 5.06374 5.61444C4.98889 5.79875 4.9707 6.0012 5.0115 6.1959C5.05231 6.39061 5.15025 6.56872 5.29281 6.70747L10.5858 12.0004L5.29281 17.2934C5.19684 17.3855 5.12021 17.4959 5.06743 17.618C5.01464 17.7402 4.98676 17.8716 4.9854 18.0046C4.98405 18.1377 5.00926 18.2697 5.05954 18.3928C5.10983 18.516 5.1842 18.6279 5.27827 18.722C5.37235 18.8161 5.48426 18.8904 5.60743 18.9407C5.73061 18.991 5.86259 19.0162 5.99563 19.0149C6.12867 19.0135 6.2601 18.9856 6.38223 18.9328C6.50436 18.8801 6.61473 18.8034 6.70687 18.7075L11.9998 13.4145L17.2928 18.7075C17.385 18.8034 17.4953 18.8801 17.6175 18.9328C17.7396 18.9856 17.871 19.0135 18.0041 19.0149C18.1371 19.0162 18.2691 18.991 18.3922 18.9407C18.5154 18.8904 18.6273 18.8161 18.7214 18.722C18.8155 18.6279 18.8899 18.516 18.9401 18.3928C18.9904 18.2697 19.0156 18.1377 19.0143 18.0047C19.0129 17.8716 18.985 17.7402 18.9323 17.618C18.8795 17.4959 18.8028 17.3855 18.7069 17.2934L13.4139 12.0004L18.7069 6.70747C18.8514 6.567 18.9501 6.38616 18.99 6.18864C19.03 5.99112 19.0093 5.78614 18.9308 5.60055C18.8523 5.41497 18.7195 5.25744 18.5499 5.14861C18.3803 5.03978 18.1817 4.98473 17.9803 4.99067C17.7205 4.99841 17.4739 5.10699 17.2928 5.2934L11.9998 10.5864L6.70687 5.2934C6.61368 5.19761 6.50224 5.12146 6.37912 5.06947C6.25601 5.01747 6.12372 4.99068 5.99007 4.99067Z", fill: "#595959" }))),
                React__default["default"].createElement(StyledDrawerContent, { onClick: (e) => {
                        if (isPropagationEvent) {
                            e.stopPropagation();
                        }
                    } }, children &&
                    (React__default["default"].isValidElement(children)
                        ? children
                        : typeof children === 'function'
                            ? children({
                                internalClose: onCloseDrawer,
                            })
                            : null))))));
};

exports.Drawer = Drawer;
