"use strict";

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

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

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

var _Changeable = _interopRequireDefault(require("@enact/ui/Changeable"));

var _Pure = _interopRequireDefault(require("@enact/ui/internal/Pure"));

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

var _clamp = _interopRequireDefault(require("ramda/src/clamp"));

var _compose = _interopRequireDefault(require("ramda/src/compose"));

var _react = require("react");

var _Picker = _interopRequireWildcard(require("../internal/Picker"));

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

/**
 * The base `Picker` component.
 *
 * This version is not [`spottable`]{@link spotlight/Spottable.Spottable}.
 *
 * @class PickerBase
 * @memberof agate/Picker
 * @ui
 * @public
 */
var PickerBase = (0, _kind["default"])({
  name: 'Picker',
  propTypes:
  /** @lends agate/Picker.PickerBase.prototype */
  {
    /**
     * Picker value list.
     *
     * @type {Array}
     * @required
     * @public
     */
    children: _propTypes["default"].array.isRequired,

    /**
     * Overrides the `aria-valuetext` for the picker. By default, `aria-valuetext` is set
     * to the current value. This should only be used when the parent controls the value of
     * the picker directly through the props.
     *
     * @type {String}
     * @public
     */
    'aria-label': _propTypes["default"].string,

    /**
     * Sets the hint string read when focusing the decrement button.
     *
     * @default 'previous item'
     * @type {String}
     * @public
     */
    decrementAriaLabel: _propTypes["default"].string,

    /**
     * Disables the picker.
     *
     * @type {Boolean}
     * @public
     */
    disabled: _propTypes["default"].bool,

    /**
     * Sets the hint string read when focusing the increment button.
     *
     * @default 'next item'
     * @type {String}
     * @public
     */
    incrementAriaLabel: _propTypes["default"].string,

    /**
     * Disables transition animation.
     *
     * @type {Boolean}
     * @public
     */
    noAnimation: _propTypes["default"].bool,

    /**
     * Called when the `value` changes.
     *
     * @type {Function}
     * @public
     */
    onChange: _propTypes["default"].func,

    /**
     * Orientation of the picker.
     *
     * Controls whether the buttons are arranged horizontally or vertically around the value.
     *
     * * Values: `'horizontal'`, `'vertical'`
     *
     * @type {String}
     * @default 'vertical'
     * @public
     */
    orientation: _propTypes["default"].oneOf(['horizontal', 'vertical']),

    /**
     * Index of the selected child.
     *
     * @type {Number}
     * @default 0
     * @public
     */
    value: _propTypes["default"].number,

    /**
     * Allows picker to continue from the start of the list after it reaches the end and
     * vice-versa.
     *
     * @type {Boolean}
     * @public
     */
    wrap: _propTypes["default"].bool
  },
  defaultProps: {
    orientation: 'vertical',
    value: 0
  },
  computed: {
    children: function children(_ref) {
      var _children = _ref.children;
      return _react.Children.map(_children, function (child) {
        return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Picker.PickerItem, {
          children: child
        });
      });
    },
    disabled: function disabled(_ref2) {
      var children = _ref2.children,
          _disabled = _ref2.disabled;
      return _react.Children.count(children) > 1 ? _disabled : true;
    },
    max: function max(_ref3) {
      var children = _ref3.children;
      return children && children.length ? children.length - 1 : 0;
    },
    value: function value(_ref4) {
      var _value = _ref4.value,
          children = _ref4.children;
      var max = children && children.length ? children.length - 1 : 0;
      return (0, _clamp["default"])(0, max, _value);
    }
  },
  render: function render(props) {
    var children = props.children,
        max = props.max,
        value = props.value,
        rest = _objectWithoutProperties(props, ["children", "max", "value"]);

    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Picker["default"], _objectSpread(_objectSpread({}, rest), {}, {
      index: value,
      min: 0,
      max: max,
      value: value,
      children: children
    }));
  }
});
/**
 * Applies Agate specific behaviors to [PickerBase]{@link agate/Picker.PickerBase} components.
 *
 * @hoc
 * @memberof agate/Picker
 * @mixes ui/Changeable.Changeable
 * @public
 */

exports.PickerBase = PickerBase;
var PickerDecorator = (0, _compose["default"])(_Pure["default"], _Changeable["default"], _Picker.ChangeAdapter);
/**
 * A Picker component that allows selecting values from a list of values.
 *
 * By default, `Picker` maintains the state of its `value` property. Supply the `defaultValue`
 * property to control its initial value. If you wish to directly control updates to the component,
 * supply a value to `value` at creation time and update it in response to `onChange` events.
 *
 * @class Picker
 * @memberof agate/Picker
 * @extends agate/Picker.PickerBase
 * @ui
 * @public
 */

var Picker = PickerDecorator(PickerBase);
/**
 * Default index of the selected child.
 *
 * *Note*: Changing `defaultValue` after initial render has no effect.
 *
 * @name defaultValue
 * @memberof agate/Picker.Picker.prototype
 * @type {Number}
 * @public
 */

exports.Picker = Picker;
var _default = Picker;
exports["default"] = _default;