"use strict";

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

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

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

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 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 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 [ArcPicker]{@link agate/ArcPicker.ArcPicker}.
 *
 * @class ArcPickerBase
 * @memberof agate/ArcPicker
 * @ui
 * @public
 */
var 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']),

    /**
     * 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: {
    backgroundColor: '#eeeeee',
    endAngle: 310,
    foregroundColor: '#444444',
    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 backgroundColor = props.backgroundColor,
          children = props.children,
          endAngle = props.endAngle,
          foregroundColor = props.foregroundColor,
          isFocused = props.isFocused,
          onClick = props.onClick,
          radius = props.radius,
          selectionType = props.selectionType,
          startAngle = props.startAngle,
          strokeWidth = props.strokeWidth,
          value = props.value;
      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, ["arcSegments", "disabled", "slotCenter", "value"]);

    delete rest.backgroundColor;
    delete rest.children;
    delete rest.endAngle;
    delete rest.foregroundColor;
    delete rest.isFocused;
    delete rest.onClick;
    delete rest.selectionType;
    delete rest.startAngle;
    return (
      /*#__PURE__*/
      // eslint-disable-next-line jsx-a11y/role-has-required-aria-props
      (0, _jsxRuntime.jsxs)("div", _objectSpread(_objectSpread({
        "aria-disabled": disabled,
        "aria-valuetext": value,
        role: "slider"
      }, rest), {}, {
        disabled: disabled,
        children: [arcSegments, /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: _ArcPickerModule["default"].valueDisplay,
          children: slotCenter
        })]
      }))
    );
  }
});
/**
 * Applies Agate specific behaviors to [ArcPickerBase]{@link agate/ArcPicker.ArcPickerBase} components.
 *
 * @hoc
 * @memberof agate/ArcPicker
 * @mixes ui/Changeable.Changeable
 * @mixes agate/Skinnable.Skinnable
 * @public
 */

exports.ArcPickerBase = ArcPickerBase;
var ArcPickerDecorator = (0, _compose["default"])(_Changeable["default"], _ArcPickerBehaviorDecorator["default"], _Spottable["default"], _Skinnable["default"]);
/**
 * 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
 */

exports.ArcPickerDecorator = ArcPickerDecorator;
var ArcPicker = ArcPickerDecorator(ArcPickerBase);
exports.ArcPicker = ArcPicker;
var _default = ArcPicker;
exports["default"] = _default;