"use strict";

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

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

var _propTypes = _interopRequireDefault(require("@enact/core/internal/prop-types"));

var _kind = _interopRequireDefault(require("@enact/core/kind"));

var _propTypes2 = _interopRequireDefault(require("prop-types"));

var _$L = _interopRequireDefault(require("../internal/$L"));

var _Button = _interopRequireDefault(require("../Button"));

var _Skinnable = _interopRequireDefault(require("../Skinnable"));

var _ContextualPopupModule = _interopRequireDefault(require("./ContextualPopup.module.css"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": 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; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

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

/**
 * An SVG arrow for {@link agate/ContextualPopupDecorator.ContextualPopup}.
 *
 * @class ContextualPopupArrow
 * @memberof agate/ContextualPopupDecorator
 * @ui
 * @private
 */
var ContextualPopupArrow = (0, _kind["default"])({
  name: 'ContextualPopupArrow',
  propTypes:
  /** @lends agate/ContextualPopupDecorator.ContextualPopupArrow.prototype */
  {
    /**
     * Direction of ContextualPopupArrow.
     *
     * @type {('above'|'below'|'left'|'right')}
     * @default 'below'
     * @public
     */
    direction: _propTypes2["default"].oneOf(['above', 'below', 'left', 'right'])
  },
  defaultProps: {
    direction: 'below'
  },
  styles: {
    css: _ContextualPopupModule["default"],
    className: 'arrow'
  },
  computed: {
    className: function className(_ref) {
      var direction = _ref.direction,
          styler = _ref.styler;
      return styler.append(direction, _ContextualPopupModule["default"].arrow);
    }
  },
  render: function render(props) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)("svg", _objectSpread(_objectSpread({}, props), {}, {
      viewBox: "0 0 30 30",
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)("path", {
        d: "M15 2 L0 20 L30 20 Z",
        className: _ContextualPopupModule["default"].arrowFill
      })
    }));
  }
});
var ContextualPopupRoot = (0, _Skinnable["default"])('div');
/**
 * A popup component used by
 * [ContextualPopupDecorator]{@link agate/ContextualPopupDecorator.ContextualPopupDecorator} to
 * wrap its [popupComponent]{@link agate/ContextualPopupDecorator.ContextualPopupDecorator.popupComponent}.
 *
 * `ContextualPopup` is usually not used directly but is made available for unique application use
 * cases.
 *
 * @class ContextualPopup
 * @memberof agate/ContextualPopupDecorator
 * @ui
 * @public
 */

var ContextualPopupBase = (0, _kind["default"])({
  name: 'ContextualPopup',
  propTypes:
  /** @lends agate/ContextualPopupDecorator.ContextualPopup.prototype */
  {
    /**
     * The contents of the popup.
     *
     * @type {Node}
     * @required
     * @public
     */
    children: _propTypes2["default"].node.isRequired,

    /**
     * Style object for arrow position.
     *
     * @type {Object}
     * @public
     */
    arrowPosition: _propTypes2["default"].shape({
      bottom: _propTypes2["default"].number,
      left: _propTypes2["default"].number,
      right: _propTypes2["default"].number,
      top: _propTypes2["default"].number
    }),

    /**
     * Style object for container position.
     *
     * @type {Object}
     * @public
     */
    containerPosition: _propTypes2["default"].shape({
      bottom: _propTypes2["default"].number,
      left: _propTypes2["default"].number,
      right: _propTypes2["default"].number,
      top: _propTypes2["default"].number
    }),

    /**
     * Called with the reference to the container node.
     *
     * @type {Object|Function}
     * @public
     */
    containerRef: _propTypes["default"].ref,

    /**
     * Direction of ContextualPopup.
     *
     * @type {('above'|'above center'|'above left'|'above right'|'below'|'below center'|'below left'|'below right'|'left middle'|'left top'|'left bottom'|'right middle'|'right top'|'right bottom')}
     * @default 'below center'
     * @public
     */
    direction: _propTypes2["default"].oneOf(['above', 'above center', 'above left', 'above right', 'below', 'below center', 'below left', 'below right', 'left middle', 'left top', 'left bottom', 'right middle', 'right top', 'right bottom']),

    /**
     * Hides the arrow.
     *
     * @type {Boolean}
     * @public
     */
    noArrow: _propTypes2["default"].bool,

    /**
     * Offset from the activator to apply to the position of the popup.
     *
     * @type {('none'|'overlap'|'small')}
     * @default 'small'
     * @public
     */
    offset: _propTypes2["default"].oneOf(['none', 'overlap', 'small']),

    /**
     * Called when the close button is clicked.
     *
     * @type {Function}
     * @public
     */
    onCloseButtonClick: _propTypes2["default"].func,

    /**
     * Shows the close button.
     *
     * @type {Boolean}
     * @default false
     * @public
     */
    showCloseButton: _propTypes2["default"].bool
  },
  defaultProps: {
    direction: 'below center',
    offset: 'small',
    showCloseButton: false
  },
  styles: {
    css: _ContextualPopupModule["default"],
    className: 'container'
  },
  computed: {
    arrowDirection: function arrowDirection(_ref2) {
      var direction = _ref2.direction;

      var _direction$split = direction.split(' '),
          _direction$split2 = _slicedToArray(_direction$split, 1),
          arrowDirection = _direction$split2[0];

      return arrowDirection;
    },
    className: function className(_ref3) {
      var direction = _ref3.direction,
          offset = _ref3.offset,
          showCloseButton = _ref3.showCloseButton,
          styler = _ref3.styler;
      return styler.append({
        fixedSize: direction === 'above' || direction === 'below'
      }, direction.split(' '), {
        reserveClose: showCloseButton
      }, offset);
    },
    closeButton: function closeButton(_ref4) {
      var showCloseButton = _ref4.showCloseButton,
          onCloseButtonClick = _ref4.onCloseButtonClick;

      if (showCloseButton) {
        return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Button["default"], {
          "aria-label": (0, _$L["default"])('Close'),
          backgroundOpacity: "transparent",
          className: _ContextualPopupModule["default"].closeButton,
          css: _ContextualPopupModule["default"],
          icon: "closex",
          onTap: onCloseButtonClick,
          size: "small"
        });
      }
    }
  },
  render: function render(_ref5) {
    var arrowDirection = _ref5.arrowDirection,
        arrowPosition = _ref5.arrowPosition,
        children = _ref5.children,
        className = _ref5.className,
        closeButton = _ref5.closeButton,
        containerPosition = _ref5.containerPosition,
        containerRef = _ref5.containerRef,
        noArrow = _ref5.noArrow,
        rest = _objectWithoutProperties(_ref5, ["arrowDirection", "arrowPosition", "children", "className", "closeButton", "containerPosition", "containerRef", "noArrow"]);

    delete rest.direction;
    delete rest.offset;
    delete rest.onCloseButtonClick;
    delete rest.showCloseButton;
    return /*#__PURE__*/(0, _jsxRuntime.jsxs)(ContextualPopupRoot, _objectSpread(_objectSpread({
      "aria-live": "off",
      role: "alert"
    }, rest), {}, {
      className: _ContextualPopupModule["default"].contextualPopup,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
        className: className,
        style: containerPosition,
        ref: containerRef,
        children: [children, closeButton]
      }), noArrow ? null : /*#__PURE__*/(0, _jsxRuntime.jsx)(ContextualPopupArrow, {
        direction: arrowDirection,
        style: arrowPosition
      })]
    }));
  }
});
exports.ContextualPopupBase = exports.ContextualPopup = ContextualPopupBase;
var _default = ContextualPopupBase;
exports["default"] = _default;