"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.PopupDecorator = exports.PopupBase = exports.Popup = exports["default"] = void 0;

var _jsxRuntime = require("react/jsx-runtime");

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"));

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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 _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

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; }

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 = (0, _kind["default"])({
  name: 'Popup',
  propTypes:
  /** @lends agate/Popup.PopupBase.prototype */
  {
    /**
     * 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,

    /**
     * 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,

    /**
     * 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 current skin.
     *
     * @type {String}
     * @private
     */
    skin: _propTypes["default"].string,

    /**
     * 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',
    noAnimation: false,
    open: false,
    position: 'center'
  },
  styles: {
    css: _PopupModule["default"],
    className: 'popup',
    publicClassNames: ['popup', 'body', 'popupTransitionContainer', 'top', 'right', 'bottom', 'left']
  },
  computed: {
    className: function className(_ref) {
      var centered = _ref.centered,
          closeButton = _ref.closeButton,
          position = _ref.position,
          styler = _ref.styler,
          title = _ref.title;
      return styler.append(position, {
        withCloseButton: closeButton,
        withTitle: title,
        centered: centered
      });
    },
    transitionType: function transitionType(_ref2) {
      var position = _ref2.position,
          type = _ref2.type;
      var setTransitionType = position === 'center' ? 'fade' : 'slide';
      return type ? type : setTransitionType;
    },
    direction: function direction(_ref3) {
      var position = _ref3.position;
      return transitionDirection[position];
    }
  },
  render: function render(_ref4) {
    var buttons = _ref4.buttons,
        children = _ref4.children,
        closeButton = _ref4.closeButton,
        css = _ref4.css,
        direction = _ref4.direction,
        duration = _ref4.duration,
        noAnimation = _ref4.noAnimation,
        onClose = _ref4.onClose,
        onHide = _ref4.onHide,
        open = _ref4.open,
        skin = _ref4.skin,
        title = _ref4.title,
        transitionType = _ref4.transitionType,
        rest = _objectWithoutProperties(_ref4, ["buttons", "children", "closeButton", "css", "direction", "duration", "noAnimation", "onClose", "onHide", "open", "skin", "title", "transitionType"]);

    var wideLayout = skin === 'carbon';
    delete rest.centered;
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(TransitionContainer, {
      noAnimation: noAnimation,
      visible: open,
      direction: direction,
      duration: duration,
      type: transitionType,
      className: css.popupTransitionContainer,
      onHide: onHide,
      css: css,
      children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", _objectSpread(_objectSpread({
        role: "alert"
      }, rest), {}, {
        children: [closeButton ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_Button["default"], {
          icon: "closex",
          onTap: onClose,
          className: _PopupModule["default"].closeButton,
          size: "small"
        }) : 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 [PopupBase]{@link agate/Popup.PopupBase}.
 *
 * @hoc
 * @memberof agate/Popup
 * @mixes ui/Slottable.Slottable
 * @mixes agate/Skinnable.Skinnable
 * @public
 */

exports.PopupBase = PopupBase;
var 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
 * [FloatingLayer]{@link ui/FloatingLayer.FloatingLayer}.
 *
 * @class Popup
 * @memberof agate/Popup
 * @extends agate/Popup.PopupBase
 * @mixes agate/Popup.PopupDecorator
 * @ui
 * @public
 */

exports.PopupDecorator = PopupDecorator;
var Popup = PopupDecorator(PopupBase);
exports.Popup = Popup;
var _default = Popup;
exports["default"] = _default;