"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = exports.ProgressBarTooltipBase = exports.ProgressBarTooltip = void 0;
var _kind = _interopRequireDefault(require("@enact/core/kind"));
var _util = require("@enact/core/util");
var _i18n = _interopRequireDefault(require("@enact/i18n"));
var _I18nDecorator = require("@enact/i18n/I18nDecorator");
var _NumFmt = _interopRequireDefault(require("ilib/lib/NumFmt"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _Tooltip = _interopRequireDefault(require("../TooltipDecorator/Tooltip"));
var _ProgressBarTooltipModule = _interopRequireDefault(require("./ProgressBarTooltip.module.css"));
var _jsxRuntime = require("react/jsx-runtime");
var _excluded = ["children", "css", "visible"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
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; }
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 _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _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(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
var verticalPositions = ['before', 'after', 'left', 'right'];

// prop-type validator that warns on invalid orientation + position
/* istanbul ignore next */
var validatePosition = function validatePosition(base) {
  return function (props, key, componentName, location, propFullName) {
    var position = props.position;
    for (var _len = arguments.length, rest = new Array(_len > 5 ? _len - 5 : 0), _key = 5; _key < _len; _key++) {
      rest[_key - 5] = arguments[_key];
    }
    var result = base.apply(void 0, [props, key, componentName, location, propFullName].concat(rest));
    if (!result && position) {
      var orientation = props.orientation || 'horizontal';
      var hasVerticalValue = verticalPositions.includes(position);
      if (orientation === 'vertical' && !hasVerticalValue || orientation === 'horizontal' && hasVerticalValue) {
        result = new Error("'".concat(key, "' value '").concat(position, "' is not a valid value for the orientation '").concat(orientation, "'"));
      }
    }
    return result;
  };
};
var memoizedPercentFormatter = (0, _util.memoize)(function /* locale */
() {
  return new _NumFmt["default"]({
    type: 'percentage',
    useNative: false
  });
});
var getDefaultPosition = function getDefaultPosition(orientation) {
  return orientation === 'horizontal' ? 'above' : 'after';
};

// Returns an array of keywords with horizontal first and vertical second
var getSide = function getSide(orientation, position) {
  position = position || getDefaultPosition(orientation);
  if (orientation === 'horizontal') {
    switch (position) {
      case 'above':
      case 'below':
        return ['auto', position];
      case 'above after':
      case 'above before':
      case 'above center':
      case 'above left':
      case 'above right':
      case 'below after':
      case 'below before':
      case 'below center':
      case 'below left':
      case 'below right':
        return position.split(' ').reverse();
      default:
        // invalid values for horizontal so use defaults
        return ['auto', 'above'];
    }
  } else {
    switch (position) {
      case 'after':
      case 'before':
      case 'left':
      case 'right':
        return [position, 'above'];
      default:
        // invalid values for horizontal so use defaults
        return ['after', 'auto'];
    }
  }
};

/**
 * A {@link agate/TooltipDecorator.Tooltip|Tooltip} specifically adapted for use with
 * {@link agate/ProgressBar.ProgressBar|ProgressBar},
 * {@link agate/Slider.Slider|Slider} or {@link agate/IncrementSlider.IncrementSlider|IncrementSlider}.
 *
 * @class ProgressBarTooltip
 * @memberof agate/ProgressBar
 * @ui
 * @public
 */
var ProgressBarTooltipBase = exports.ProgressBarTooltipBase = (0, _kind["default"])({
  name: 'ProgressBarTooltip',
  propTypes: /** @lends agate/ProgressBar.ProgressBarTooltip.prototype */{
    /**
     * 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:
     *
     * * `tooltip` - The root component class
     *
     * @type {Object}
     * @public
     */
    css: _propTypes["default"].object,
    /**
     * Sets the orientation of the tooltip based on the orientation of the bar.
     *
     * 'vertical' sends the tooltip to one of the sides, 'horizontal' positions it above the bar.
     *
     * @type {('horizontal'|'vertical')}
     * @default 'horizontal'
     * @public
     */
    orientation: _propTypes["default"].oneOf(['horizontal', 'vertical']),
    /**
     * Displays the value as a percentage.
     *
     * @type {Boolean}
     * @default false
     * @public
     */
    percent: _propTypes["default"].bool,
    /**
     * Position of the tooltip with respect to the progress bar.
     *
     * * For `orientation="horizontal"` progress bars, the default value is `'above'`.
     * * For `orientation="vertical"` progress bars, the default value is `'before'`.
     *
     * When using `'before'` or `'after'` alone or in any of the below combinations, `'before'`
     * will position the tooltip on the side of the current locale's text directionality. In LTR
     * locales, it will be on the left; in RTL locales, it will be on the right. Similarly,
     * `'after'` will position the tooltip on the opposite side: the right side for LTR and
     * left for RTL.
     *
     * Valid values when `orientation="horizontal"`
     *
     * | *Value* | *Tooltip Direction* |
     * |---|---|
     * | `'above'` | Above component, flowing to the nearest end |
     * | `'above left'` | Above component, flowing to the left |
     * | `'above before'` | Above component, flowing to the start of text |
     * | `'above center'` | Above component, flowing to the center |
     * | `'above right'` | Above component, flowing to the right |
     * | `'above after'` | Above component, flowing to the end of text |
     * | `'below'` | Below component, flowing to the nearest end |
     * | `'below left'` | Below component, flowing to the left |
     * | `'below before'` | Below component, flowing to the start of text |
     * | `'below center'` | Below component, flowing to the center |
     * | `'below right'` | Below component, flowing to the right |
     * | `'below after'` | Below component, flowing to the end of text |
     *
     * Valid values when `orientation="vertical"`
     *
     * | *Value* | *Tooltip Direction* |
     * |---|---|
     * | `'left'` | Left of the component, contents middle aligned |
     * | `'before'` | Start of text side of the component, contents middle aligned |
     * | `'right'` | right of the component, contents middle aligned |
     * | `'after'` | End of text side of the component, contents middle aligned |
     *
     * @type {('above'|'above before'|'above left'|'above after'|'above center'|'above right'|'below'|'below left'|'below before'|'below center'|'below right'|'below after'|'left'|'before'|'right'|'after')}
     * @public
     */
    position: validatePosition(_propTypes["default"].oneOf([
    // horizontal
    'above', 'above before', 'above left', 'above center', 'above after', 'above right', 'below', 'below left', 'below before', 'below center', 'below right', 'below after',
    // vertical
    'left', 'before', 'right', 'after'])),
    /**
     * The proportion of the filled part of the bar.
     *
     * * Should be a number between 0 and 1.
     *
     * @type {Number}
     * @default 0
     * @public
     */
    proportion: _propTypes["default"].number,
    /**
     * Sets the text direction to be right-to-left
     *
     * @type {Boolean}
     * @private
     */
    rtl: _propTypes["default"].bool,
    /**
     * Visibility of the tooltip
     *
     * @type {Boolean}
     * @default false
     * @public
     */
    visible: _propTypes["default"].bool
  },
  defaultProps: {
    orientation: 'horizontal',
    percent: false,
    proportion: 0,
    visible: false
  },
  styles: {
    css: _ProgressBarTooltipModule["default"],
    className: 'tooltip',
    publicClassNames: true
  },
  computed: {
    children: function children(_ref) {
      var _children = _ref.children,
        proportion = _ref.proportion,
        percent = _ref.percent;
      if (percent) {
        var formatter = memoizedPercentFormatter(_i18n["default"].getLocale());
        return formatter.format(Math.round(proportion * 100));
      }
      return _children;
    },
    className: function className(_ref2) {
      var orientation = _ref2.orientation,
        position = _ref2.position,
        proportion = _ref2.proportion,
        styler = _ref2.styler;
      var _getSide = getSide(orientation, position),
        _getSide2 = _slicedToArray(_getSide, 2),
        h = _getSide2[0],
        v = _getSide2[1];
      return styler.append(orientation, {
        above: v === 'above',
        below: v === 'below',
        before: h === 'before',
        after: h === 'after',
        center: h === 'center',
        left: h === 'left' || h === 'auto' && proportion <= 0.5,
        right: h === 'right' || h === 'auto' && proportion > 0.5
      });
    },
    arrowAnchor: function arrowAnchor(_ref3) {
      var orientation = _ref3.orientation,
        position = _ref3.position,
        rtl = _ref3.rtl;
      if (orientation === 'vertical') return 'middle';
      var _getSide3 = getSide(orientation, position),
        _getSide4 = _slicedToArray(_getSide3, 1),
        h = _getSide4[0];
      switch (h) {
        case 'auto':
          return 'center';
        case 'before':
          return rtl ? 'right' : 'left';
        case 'after':
          return rtl ? 'left' : 'right';
        case 'left':
        case 'right':
        case 'center':
          return h;
      }
    },
    direction: function direction(_ref4) {
      var orientation = _ref4.orientation,
        position = _ref4.position,
        rtl = _ref4.rtl;
      var _getSide5 = getSide(orientation, position),
        _getSide6 = _slicedToArray(_getSide5, 2),
        h = _getSide6[0],
        v = _getSide6[1];
      var dir = 'right';
      if (orientation === 'vertical') {
        if (
        // forced to the left
        h === 'left' ||
        // LTR before
        !rtl && h === 'before' ||
        // RTL after
        rtl && h === 'after') {
          dir = 'left';
        }
      } else {
        dir = v !== 'below' ? 'above' : 'below';
      }
      return dir;
    },
    style: function style(_ref5) {
      var proportion = _ref5.proportion,
        _style = _ref5.style;
      return _objectSpread(_objectSpread({}, _style), {}, {
        '--tooltip-progress-proportion': proportion
      });
    }
  },
  render: function render(_ref6) {
    var children = _ref6.children,
      css = _ref6.css,
      visible = _ref6.visible,
      rest = _objectWithoutProperties(_ref6, _excluded);
    if (!visible) return null;
    delete rest.orientation;
    delete rest.percent;
    delete rest.position;
    delete rest.proportion;
    delete rest.rtl;
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Tooltip["default"], _objectSpread(_objectSpread({}, rest), {}, {
      css: css,
      children: children
    }));
  }
});
var ProgressBarTooltip = exports.ProgressBarTooltip = (0, _I18nDecorator.I18nContextDecorator)({
  rtlProp: 'rtl'
}, ProgressBarTooltipBase);
ProgressBarTooltip.defaultSlot = 'tooltip';
var _default = exports["default"] = ProgressBarTooltip;