"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = exports.PopupDecorator = exports.PopupBase = exports.Popup = void 0;
var _kind = _interopRequireDefault(require("@enact/core/kind"));
var _SpotlightContainerDecorator = _interopRequireDefault(require("@enact/spotlight/SpotlightContainerDecorator"));
var _Layout = _interopRequireWildcard(require("@enact/ui/Layout"));
var _Slottable = _interopRequireDefault(require("@enact/ui/Slottable"));
var _Transition = _interopRequireDefault(require("@enact/ui/Transition"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _compose = _interopRequireDefault(require("ramda/src/compose"));
var _Button = _interopRequireDefault(require("../Button"));
var _Heading = _interopRequireDefault(require("../Heading"));
var _Skinnable = _interopRequireDefault(require("../Skinnable"));
var _PopupState = _interopRequireDefault(require("./PopupState"));
var _PopupModule = _interopRequireDefault(require("./Popup.module.css"));
var _jsxRuntime = require("react/jsx-runtime");
var _excluded = ["buttons", "children", "closeButton", "css", "direction", "duration", "noAnimation", "onClose", "onHide", "onShow", "open", "skin", "spotlightId", "spotlightRestrict", "title", "transitionContainerClassName", "transitionType"];
/**
 * Modal component that appears at the bottom of the screen and takes up the full screen width.
 *
 * @example
 * <Popup open>Hello!</Popup>
 *
 * @module agate/Popup
 * @exports Popup
 * @exports PopupBase
 * @exports PopupDecorator
 */
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var s = Object.getOwnPropertySymbols(e); for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; }
var TransitionContainer = (0, _SpotlightContainerDecorator["default"])({
  enterTo: 'default-element',
  preserveId: true
}, _Transition["default"]);
var transitionDirection = {
  bottom: 'down',
  center: 'down',
  fullscreen: 'down',
  left: 'left',
  right: 'right',
  top: 'up'
};

/**
 * The base popup component.
 *
 * @class PopupBase
 * @memberof agate/Popup
 * @ui
 * @public
 */
var PopupBase = exports.PopupBase = (0, _kind["default"])({
  name: 'PopupBase',
  propTypes: /** @lends agate/Popup.PopupBase.prototype */{
    /**
     * Set the priority with which screen reader should treat updates to live regions for the Popup.
     *
     * @type {String|Object}
     * @public
     */
    'aria-live': _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].object]),
    /**
     * Buttons to be included within the Popup component.
     *
     * Typically, these buttons would be used to close or take action on the dialog.
     *
     * @type {Element|Element[]}
     * @public
     */
    buttons: _propTypes["default"].oneOfType([_propTypes["default"].element, _propTypes["default"].arrayOf(_propTypes["default"].element)]),
    /**
     * Centers the contents.
     *
     * @type {Boolean}
     * @default false
     * @public
     */
    centered: _propTypes["default"].bool,
    /**
     * When true, popup displays a close button.
     *
     * @type {Boolean}
     * @default false
     * @public
     */
    closeButton: _propTypes["default"].bool,
    /**
     * Customizes the component by mapping the supplied collection of CSS class names to the
     * corresponding internal elements and states of this component.
     *
     * The following classes are supported:
     *
     * * `popup` - The root class name
     * * `body` - Applied to the body content container
     * * `popupTransitionContainer` - Applied to the Popup's outermost container. Sizing can be
     *                                applied here for percentage-of-screen values.
     * * `top` - Applied when the `position` is 'top'
     * * `right` - Applied when the `position` is 'right'
     * * `bottom` - Applied when the `position` is 'bottom'
     * * `left` - Applied when the `position` is 'left'
     *
     * @type {Object}
     * @public
     */
    css: _propTypes["default"].object,
    /**
     * Controls how long the transition should take.
     * Supported preset durations are: `'short'` (250ms), `'medium'` (500ms), and `'long'` (1s).
     * `'short'` (250ms) is default when no others are specified.
     *
     * @type {String}
     * @default 'short'
     * @public
     */
    duration: _propTypes["default"].string,
    /**
     * Support accessibility options.
     *
     * If true, the aria-live and role in Popup are `null`.
     *
     * @type {Boolean}
     * @default false
     * @private
     */
    noAlertRole: _propTypes["default"].bool,
    /**
     * Disables transition animation.
     *
     * @type {Boolean}
     * @default false
     * @public
     */
    noAnimation: _propTypes["default"].bool,
    /**
     * Called when the popup is closed.
     *
     * @type {Function}
     * @public
     */
    onClose: _propTypes["default"].func,
    /**
     * Called after the popup's "hide" transition finishes.
     *
     * @type {Function}
     * @public
     */
    onHide: _propTypes["default"].func,
    /**
     * Called after the popup's "show" transition finishes.
     *
     * @type {Function}
     * @public
     */
    onShow: _propTypes["default"].func,
    /**
     * Controls the visibility of the Popup.
     *
     * By default, the Popup and its contents are not rendered until `open`.
     *
     * @type {Boolean}
     * @default false
     * @public
     */
    open: _propTypes["default"].bool,
    /**
     * Sets the position of the popup on the screen.
     *
     * @type {('bottom'|'center'|'fullscreen'|'left'|'right'|'top')}
     * @default 'center'
     * @public
     */
    position: _propTypes["default"].oneOf(['bottom', 'center', 'fullscreen', 'left', 'right', 'top']),
    /**
     * The ARIA role for the Popup.
     *
     * @type {String|Object}
     * @public
     */
    role: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].object]),
    /**
     * The current skin.
     *
     * @type {String}
     * @private
     */
    skin: _propTypes["default"].string,
    /**
     * The container id for {@link spotlight/Spotlight}.
     *
     * @type {String}
     * @default null
     * @public
     */
    spotlightId: _propTypes["default"].string,
    /**
     * Restricts or prioritizes navigation when focus attempts to leave the popup.
     *
     * It can be either `'none'`, `'self-first'`, or `'self-only'`.
     *
     * Note: The ready-to-use {@link agate/Popup.Popup|Popup} component only supports
     * `'self-first'` and `'self-only'`.
     *
     * @type {('none'|'self-first'|'self-only')}
     * @default 'self-only'
     * @public
     */
    spotlightRestrict: _propTypes["default"].oneOf(['none', 'self-first', 'self-only']),
    /**
     * The primary text of the popup.
     *
     * @type {String}
     * @public
     */
    title: _propTypes["default"].string,
    /**
     * The type of transition to affect the content.
     *
     * Supported types are: `'slide'` and `'fade'`.
     *
     * @type {String}
     * @public
     */
    type: _propTypes["default"].oneOf(['fade', 'slide'])
  },
  defaultProps: {
    centered: false,
    closeButton: false,
    duration: 'short',
    noAlertRole: false,
    noAnimation: false,
    open: false,
    position: 'center',
    spotlightRestrict: 'self-only'
  },
  styles: {
    css: _PopupModule["default"],
    className: 'popup',
    publicClassNames: ['popup', 'body', 'content', 'popupTransitionContainer', 'top', 'right', 'bottom', 'left']
  },
  computed: {
    // When passing `aria-live` prop to the Popup, the prop should work first.
    // If `noAlertRole` is true, alert role and aria-live will be removed. Contents of the popup won't be read automatically when opened.
    // Otherwise, `aria-live` will be usually `off`.
    'aria-live': function ariaLive(_ref) {
      var live = _ref['aria-live'],
        noAlertRole = _ref.noAlertRole;
      return typeof live !== 'undefined' ? live : !noAlertRole && 'off' || null;
    },
    className: function className(_ref2) {
      var centered = _ref2.centered,
        closeButton = _ref2.closeButton,
        position = _ref2.position,
        styler = _ref2.styler,
        title = _ref2.title;
      return styler.append(position, {
        withCloseButton: closeButton,
        withTitle: title,
        centered: centered
      });
    },
    direction: function direction(_ref3) {
      var position = _ref3.position;
      return transitionDirection[position];
    },
    // When passing `role` prop to the Popup, the prop should work first.
    // If `noAlertRole` is true, alert role and aria-live will be removed. Contents of the popup won't be read automatically when opened.
    // Otherwise, `role` will be usually `alert`.
    role: function role(_ref4) {
      var noAlertRole = _ref4.noAlertRole,
        _role = _ref4.role;
      return typeof _role !== 'undefined' ? _role : !noAlertRole && 'alert' || null;
    },
    transitionContainerClassName: function transitionContainerClassName(_ref5) {
      var css = _ref5.css,
        position = _ref5.position,
        styler = _ref5.styler;
      return styler.join(css.popupTransitionContainer, position);
    },
    transitionType: function transitionType(_ref6) {
      var position = _ref6.position,
        type = _ref6.type;
      var setTransitionType = position === 'center' ? 'fade' : 'slide';
      return type ? type : setTransitionType;
    }
  },
  render: function render(_ref7) {
    var buttons = _ref7.buttons,
      children = _ref7.children,
      closeButton = _ref7.closeButton,
      css = _ref7.css,
      direction = _ref7.direction,
      duration = _ref7.duration,
      noAnimation = _ref7.noAnimation,
      onClose = _ref7.onClose,
      onHide = _ref7.onHide,
      onShow = _ref7.onShow,
      open = _ref7.open,
      skin = _ref7.skin,
      spotlightId = _ref7.spotlightId,
      spotlightRestrict = _ref7.spotlightRestrict,
      title = _ref7.title,
      transitionContainerClassName = _ref7.transitionContainerClassName,
      transitionType = _ref7.transitionType,
      rest = _objectWithoutProperties(_ref7, _excluded);
    var closeButtonSize = skin === 'silicon' ? 'large' : 'small';
    var wideLayout = skin === 'carbon';
    delete rest.centered;
    delete rest.noAlertRole;
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(TransitionContainer, {
      className: transitionContainerClassName,
      css: css,
      direction: direction,
      duration: duration,
      noAnimation: noAnimation,
      onHide: onHide,
      onShow: onShow,
      spotlightId: spotlightId,
      spotlightRestrict: spotlightRestrict,
      type: transitionType,
      visible: open,
      children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", _objectSpread(_objectSpread({
        role: "alert"
      }, rest), {}, {
        children: [closeButton ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_Button["default"], {
          className: _PopupModule["default"].closeButton,
          css: css,
          icon: "closex",
          onTap: onClose,
          size: closeButtonSize
        }) : null, title ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_Heading["default"], {
          className: css.title,
          children: title
        }) : null, /*#__PURE__*/(0, _jsxRuntime.jsxs)(_Layout["default"], {
          orientation: wideLayout ? 'horizontal' : 'vertical',
          className: css.body,
          children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Layout.Cell, {
            shrink: !wideLayout,
            className: css.content,
            children: children
          }), buttons ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_Layout.Cell, {
            shrink: true,
            className: css.buttons,
            children: buttons
          }) : null]
        })]
      }))
    });
  }
});

/**
 * Applies Agate specific behaviors to {@link agate/Popup.PopupBase|PopupBase}.
 *
 * @hoc
 * @memberof agate/Popup
 * @mixes ui/Slottable.Slottable
 * @mixes agate/Skinnable.Skinnable
 * @public
 */
var PopupDecorator = exports.PopupDecorator = (0, _compose["default"])((0, _Slottable["default"])({
  slots: ['closeButton', 'title', 'buttons']
}), (0, _Skinnable["default"])({
  prop: 'skin'
}), _PopupState["default"]);

/**
 * A stateful component that renders a popup in a
 * {@link ui/FloatingLayer.FloatingLayer|FloatingLayer}.
 *
 * @class Popup
 * @memberof agate/Popup
 * @extends agate/Popup.PopupBase
 * @mixes agate/Popup.PopupDecorator
 * @ui
 * @public
 */
var Popup = exports.Popup = PopupDecorator(PopupBase);
var _default = exports["default"] = Popup;