"use strict";

exports.__esModule = true;
exports.TagsComponentType = void 0;
exports.default = Tags;

var _react = _interopRequireWildcard(require("react"));

var _styles = require("@mui/material/styles");

var _ClickAwayListener = _interopRequireDefault(require("@mui/material/ClickAwayListener"));

var _Grow = _interopRequireDefault(require("@mui/material/Grow"));

var _Paper = _interopRequireDefault(require("@mui/material/Paper"));

var _Popper = _interopRequireDefault(require("@mui/material/Popper"));

var _Icon = _interopRequireDefault(require("@mui/material/Icon"));

var _TagChip = _interopRequireDefault(require("../TagChip"));

var _material = require("@mui/material");

const _excluded = ["tags", "title", "type", "onOpen", "onClose", "onClickTag"];

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

const PREFIX = 'SCTags';
const TagsPopperRoot = (0, _styles.styled)(_material.Box, {
  name: `${PREFIX}`,
  slot: 'Root',
  overridesResolver: (props, styles) => styles.root
})(() => ({
  cursor: 'pointer'
}));
/**
 * Custom Popper
 */

const classes = {
  paper: `${PREFIX}-paper`
};
const TagsPopper = (0, _styles.styled)(_Popper.default, {
  name: `${PREFIX}Popper`,
  slot: 'Root',
  overridesResolver: (props, styles) => styles.root
})(() => ({
  zIndex: 2,
  [`& .${classes.paper}`]: {
    padding: '10px 5px'
  }
}));
/**
 * Custom StackList and ItemList
 */

const ListRoot = (0, _styles.styled)(_material.Stack, {
  name: `${PREFIX}List`,
  slot: 'Root',
  overridesResolver: (props, styles) => styles.root
})({});
const StackList = (0, _styles.styled)(_material.Stack, {
  name: `${PREFIX}Stack`,
  slot: 'Root',
  overridesResolver: (props, styles) => styles.root
})({});
const ItemList = (0, _styles.styled)(_material.Box, {
  name: `${PREFIX}ItemList`,
  slot: 'Root',
  overridesResolver: (props, styles) => styles.root
})(({
  theme
}) => ({
  padding: theme.spacing(0.2),
  textAlign: 'center',
  color: theme.palette.text.secondary
}));
let TagsComponentType;
exports.TagsComponentType = TagsComponentType;

(function (TagsComponentType) {
  TagsComponentType["LIST"] = "list";
  TagsComponentType["POPPER"] = "popper";
})(TagsComponentType || (exports.TagsComponentType = TagsComponentType = {}));

function Tags(props) {
  // PROPS
  const {
    tags = [],
    title = null,
    type = TagsComponentType.POPPER,
    onOpen = null,
    onClose = null,
    onClickTag = null
  } = props,
        rest = _objectWithoutPropertiesLoose(props, _excluded); // STATE


  const [open, setOpen] = (0, _react.useState)(false);
  let popperRef = (0, _react.useRef)(null); // HANDLERS

  function handleToggle() {
    setOpen(prevOpen => !prevOpen);

    if (rest.onOpen) {
      rest.onOpen(!open);
    }
  }

  function handleClose() {
    if (popperRef.current && popperRef.current.contains(event.target)) {
      return;
    }

    setOpen(false);

    if (rest.onClose) {
      rest.onClose();
    }
  }
  /**
   * Renders tag title
   */


  function renderTitle() {
    return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, title && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_material.Typography, {
      sx: {
        padding: 1,
        fontWeight: 600
      }
    }, title), /*#__PURE__*/_react.default.createElement(_material.Divider, null)));
  }
  /**
   * Returns focus to the button when we transitioned from !open -> open
   */


  const prevOpen = (0, _react.useRef)(open);

  _react.default.useEffect(() => {
    if (prevOpen.current === true && open === false) {
      popperRef.current.focus();
    }

    prevOpen.current = open;
  }, [open]);
  /**
   * Renders component
   */


  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, tags.length && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, type === TagsComponentType.POPPER ? /*#__PURE__*/_react.default.createElement(TagsPopperRoot, rest, /*#__PURE__*/_react.default.createElement(_Icon.default, {
    ref: popperRef,
    fontSize: "small",
    onClick: handleToggle,
    "aria-haspopup": "true"
  }, "local_offer"), /*#__PURE__*/_react.default.createElement(TagsPopper, {
    open: open,
    anchorEl: popperRef.current,
    transition: true,
    placement: "bottom-start",
    modifiers: [{
      name: 'preventOverflow',
      enabled: true
    }]
  }, ({
    TransitionProps,
    placement
  }) => /*#__PURE__*/_react.default.createElement(_Grow.default, _extends({}, TransitionProps, {
    style: {
      transformOrigin: placement === 'bottom' ? 'center top' : 'center bottom'
    }
  }), /*#__PURE__*/_react.default.createElement(_Paper.default, {
    variant: 'outlined',
    className: classes.paper
  }, /*#__PURE__*/_react.default.createElement(_ClickAwayListener.default, {
    onClickAway: handleClose
  }, /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, renderTitle(), /*#__PURE__*/_react.default.createElement(StackList, _extends({
    spacing: 2
  }, rest), tags.map(tag => /*#__PURE__*/_react.default.createElement(ItemList, {
    key: tag.id
  }, /*#__PURE__*/_react.default.createElement(_TagChip.default, {
    tag: tag,
    onClick: onClickTag
  })))))))))) : /*#__PURE__*/_react.default.createElement(ListRoot, null, renderTitle(), /*#__PURE__*/_react.default.createElement(StackList, {
    spacing: 2,
    direction: rest.direction ? rest.direction : 'column'
  }, tags.map(tag => /*#__PURE__*/_react.default.createElement(ItemList, {
    key: tag.id
  }, /*#__PURE__*/_react.default.createElement(_TagChip.default, {
    tag: tag,
    onClick: onClickTag
  })))))));
}