"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = exports.ArcPickerDecorator = exports.ArcPickerBase = exports.ArcPicker = void 0;
var _kind = _interopRequireDefault(require("@enact/core/kind"));
var _Spottable = _interopRequireDefault(require("@enact/spotlight/Spottable"));
var _Changeable = _interopRequireDefault(require("@enact/ui/Changeable"));
var _resolution = _interopRequireDefault(require("@enact/ui/resolution"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _compose = _interopRequireDefault(require("ramda/src/compose"));
var _Arc = _interopRequireDefault(require("../Arc"));
var _Skinnable = _interopRequireDefault(require("../Skinnable"));
var _ThemeDecorator = require("../ThemeDecorator");
var _ArcPickerBehaviorDecorator = _interopRequireDefault(require("./ArcPickerBehaviorDecorator"));
var _ArcPickerModule = _interopRequireDefault(require("./ArcPicker.module.css"));
var _jsxRuntime = require("react/jsx-runtime");
var _excluded = ["arcSegments", "disabled", "slotCenter", "value"];
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); } /**
 * Agate styled arc picker components and behaviors.
 *
 * @example
 * <ArcPicker backgroundColor="#cccccc">
 * 	{[1,2,3,4,5]}
 * </ArcPicker>
 *
 * @module agate/ArcPicker
 * @exports ArcPicker
 * @exports ArcPickerBase
 * @exports ArcPickerDecorator
 */ /**
 * An Agate component for displaying an arc picker.
 * This component is most often not used directly but may be composed within another component as it
 * is within {@link agate/ArcPicker.ArcPicker|ArcPicker}.
 *
 * @class ArcPickerBase
 * @memberof agate/ArcPicker
 * @ui
 * @public
 */
var ArcPickerBase = exports.ArcPickerBase = (0, _kind["default"])({
  name: 'ArcPickerBase',
  propTypes: /** @lends agate/ArcPicker.ArcPickerBase.prototype */{
    /**
     * The value options of ArcPicker.
     *
     * @type {Array}
     * @required
     * @public
     */
    children: _propTypes["default"].array.isRequired,
    /**
     * The color of the unselected arcs.
     *
     * @type {String}
     * @default #eeeeee
     * @public
     */
    backgroundColor: _propTypes["default"].string,
    /**
     * Whether or not the component is in a disabled state.
     *
     * @type {Boolean}
     * @public
     */
    disabled: _propTypes["default"].bool,
    /**
     * The end angle(in degrees) of the arc.
     *
     * The value should be between 0 and 360 and should be greater than startAngle.
     *
     * @type {Number}
     * @default 310
     * @public
     */
    endAngle: _propTypes["default"].number,
    /**
     * The color of the selected arcs.
     *
     * @type {String}
     * @default #444444
     * @public
     */
    foregroundColor: _propTypes["default"].string,
    /**
     * Whether or not the component is focused.
     *
     * @type {Boolean}
     * @private
     */
    isFocused: _propTypes["default"].bool,
    /**
     * Called when the path area is clicked.
     *
     * @type {Function}
     * @param {Object} event
     * @public
     */
    onClick: _propTypes["default"].func,
    /**
     * The radius of the arc circle.
     *
     * @type {Number}
     * @default 150
     * @public
     */
    radius: _propTypes["default"].number,
    /**
     * The selection type of ArcPicker.
     *
     * @type {('cumulative'|'single')}
     * @public
     */
    selectionType: _propTypes["default"].oneOf(['cumulative', 'single']),
    /**
     * The current skin for this component.
     *
     * @type {String}
     * @private
     */
    skin: _propTypes["default"].string,
    /**
     * State of possible skin variants.
     *
     * Used to set backgroundColor and foregroundColor.
     *
     * @type {Object}
     * @private
     */
    skinVariants: _propTypes["default"].object,
    /**
     * Nodes to be inserted in the center of the ArcPicker.
     *
     * @type {Node}
     * @public
     */
    slotCenter: _propTypes["default"].node,
    /**
     * The start angle(in degrees) of the arc.
     *
     * The value should be between 0 and 360.
     *
     * @type {Number}
     * @default 50
     * @public
     */
    startAngle: _propTypes["default"].number,
    /**
     * The stroke width of the arc picker.
     *
     * @type {Number}
     * @default 6
     * @public
     */
    strokeWidth: _propTypes["default"].number,
    /**
     * Value of ArcPicker.
     *
     * @type {Number|String}
     * @public
     */
    value: _propTypes["default"].oneOfType([_propTypes["default"].number, _propTypes["default"].string])
  },
  defaultProps: {
    endAngle: 310,
    radius: 150,
    startAngle: 50,
    strokeWidth: 6
  },
  contextType: _ThemeDecorator.ThemeContext,
  styles: {
    css: _ArcPickerModule["default"],
    className: 'arcPicker'
  },
  computed: {
    arcSegments: function arcSegments(props, context) {
      var _ref = context || {},
        accentColor = _ref.accent;
      var children = props.children,
        endAngle = props.endAngle,
        isFocused = props.isFocused,
        onClick = props.onClick,
        radius = props.radius,
        selectionType = props.selectionType,
        skin = props.skin,
        skinVariants = props.skinVariants,
        startAngle = props.startAngle,
        strokeWidth = props.strokeWidth,
        value = props.value;
      var backgroundColor = props.backgroundColor || (skinVariants && skinVariants.night ? '#444444' : '#888888');
      var foregroundColor;
      if (props.foregroundColor) {
        foregroundColor = props.foregroundColor;
      } else if (skinVariants && skinVariants.night) {
        foregroundColor = '#ffffff';
      } else if (skin === 'carbon') {
        foregroundColor = '#343434';
      } else {
        foregroundColor = '#000000';
      }
      if (!Array.isArray(children)) return [];
      return children.map(function (option, index) {
        // Calc `arcStartAngle`, `arcEndAngle` based on `startAngle` and `endAngle` for every <Arc />
        var pauseAngle = 2;
        var arcSegments = children.length;
        var arcStartAngle = startAngle + (endAngle - startAngle) / arcSegments * index;
        var arcEndAngle = startAngle + (endAngle - startAngle) / arcSegments * (index + 1) - pauseAngle;
        var color = selectionType === 'cumulative' && value > option || value === option ? isFocused && accentColor || foregroundColor : backgroundColor;
        return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Arc["default"], {
          className: _ArcPickerModule["default"].arc,
          color: color,
          endAngle: arcEndAngle,
          onClick: onClick(option),
          radius: radius,
          startAngle: arcStartAngle,
          strokeWidth: strokeWidth
        }, index);
      });
    },
    style: function style(_ref2) {
      var radius = _ref2.radius,
        _style = _ref2.style;
      var size = _resolution["default"].scaleToRem(radius * 2);
      return _objectSpread(_objectSpread({}, _style), {}, {
        height: size,
        width: size
      });
    }
  },
  render: function render(_ref3) {
    var arcSegments = _ref3.arcSegments,
      disabled = _ref3.disabled,
      slotCenter = _ref3.slotCenter,
      value = _ref3.value,
      rest = _objectWithoutProperties(_ref3, _excluded);
    delete rest.backgroundColor;
    delete rest.children;
    delete rest.endAngle;
    delete rest.foregroundColor;
    delete rest.isFocused;
    delete rest.onClick;
    delete rest.selectionType;
    delete rest.startAngle;
    delete rest.skinVariants;
    return (
      /*#__PURE__*/
      // eslint-disable-next-line jsx-a11y/role-has-required-aria-props
      (0, _jsxRuntime.jsxs)("div", _objectSpread(_objectSpread({}, rest), {}, {
        "aria-disabled": disabled,
        "aria-valuetext": value,
        disabled: disabled,
        role: "slider",
        children: [arcSegments, /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: _ArcPickerModule["default"].valueDisplay,
          children: slotCenter
        })]
      }))
    );
  }
});

/**
 * Applies Agate specific behaviors to {@link agate/ArcPicker.ArcPickerBase|ArcPickerBase} components.
 *
 * @hoc
 * @memberof agate/ArcPicker
 * @mixes ui/Changeable.Changeable
 * @mixes agate/Skinnable.Skinnable
 * @public
 */
var ArcPickerDecorator = exports.ArcPickerDecorator = (0, _compose["default"])(_Changeable["default"], _ArcPickerBehaviorDecorator["default"], _Spottable["default"], (0, _Skinnable["default"])({
  prop: 'skin',
  variantsProp: 'skinVariants'
}));

/**
 * An arc picker component, ready to use in Agate applications.
 *
 * Usage:
 * ```
 * <ArcPicker backgroundColor="#444444" endAngle={200} foregroundColor="#eeeeee" selectionType="single" startAngle={0} />
 * ```
 *
 * @class ArcPicker
 * @memberof agate/ArcPicker
 * @extends agate/ArcPicker.ArcPickerBase
 * @mixes agate/ArcPicker.ArcPickerDecorator
 * @ui
 * @public
 */
var ArcPicker = exports.ArcPicker = ArcPickerDecorator(ArcPickerBase);
var _default = exports["default"] = ArcPicker;