"use strict";

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

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

var _handle = require("@enact/core/handle");

var _keymap = require("@enact/core/keymap");

var _hoc = _interopRequireDefault(require("@enact/core/hoc"));

var _validators = require("@enact/ui/internal/validators");

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

var _react = require("react");

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

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

var isDown = (0, _keymap.is)('down');
var isUp = (0, _keymap.is)('up');
var validateRange = (0, _validators.validateRangeOnce)(function (props) {
  return props;
}, {
  'component': 'ArcPickerBehaviorDecorator'
});
/**
 * Adds Agate-specific ArcPicker behaviors.
 *
 * @class ArcPickerBehaviorDecorator
 * @hoc
 * @memberof agate/ArcPicker
 * @private
 */

var ArcPickerBehaviorDecorator = (0, _hoc["default"])(function (config, Wrapped) {
  var _class, _temp;

  return _temp = _class = /*#__PURE__*/function (_Component) {
    _inherits(_class, _Component);

    var _super = _createSuper(_class);

    function _class(_props) {
      var _this;

      _classCallCheck(this, _class);

      _this = _super.call(this, _props);

      _this.handleClick = function (value) {
        return function (ev) {
          (0, _handle.forward)('onChange', {
            value: value
          }, _this.props);
          ev.stopPropagation();
        };
      };

      _this.handleBlur = function () {
        _this.setState({
          isFocused: false
        });
      };

      _this.handleFocus = function () {
        _this.setState({
          isFocused: true
        });
      };

      _this.handleKeyDown = function (ev, props) {
        var _this$props = _this.props,
            children = _this$props.children,
            disabled = _this$props.disabled,
            valueProp = _this$props.value;
        var value = valueProp || valueProp === 0 ? valueProp : children[0];
        var index = children.findIndex(function (child) {
          return child === value;
        });
        (0, _handle.forward)('onKeyDown', ev, props);

        if (!disabled) {
          if (isDown(ev.keyCode)) {
            _this.handleClick(children[Math.max(index - 1, 0)])(ev);
          } else if (isUp(ev.keyCode)) {
            _this.handleClick(children[Math.min(index + 1, children.length - 1)])(ev);
          }
        }
      };

      _this.state = {
        isFocused: false
      };
      return _this;
    }

    _createClass(_class, [{
      key: "render",
      value: function render() {
        var _this$props2 = this.props,
            children = _this$props2.children,
            max = _this$props2.max,
            min = _this$props2.min,
            valueProp = _this$props2.value,
            rest = _objectWithoutProperties(_this$props2, ["children", "max", "min", "value"]);

        var value = valueProp || valueProp === 0 ? valueProp : children && children[0];
        delete rest.onChange;

        if (process.env.NODE_ENV !== "production") {
          var valueProps = {
            value: value,
            max: max,
            min: min
          };
          validateRange(valueProps);
        }

        return /*#__PURE__*/(0, _jsxRuntime.jsx)(Wrapped, _objectSpread(_objectSpread({}, rest), {}, {
          isFocused: this.state.isFocused,
          onBlur: this.handleBlur,
          onClick: this.handleClick,
          onFocus: this.handleFocus,
          onKeyDown: this.handleKeyDown,
          value: value,
          children: children
        }));
      }
    }]);

    return _class;
  }(_react.Component), _class.displayName = 'ArcPickerBehaviorDecorator', _class.propTypes =
  /** @lends agate/ArcPicker.ArcPickerBehaviorDecorator.prototype */
  {
    /**
     * The value options of ArcPicker.
     *
     * @type {Array}
     * @required
     * @public
     */
    children: _propTypes["default"].array.isRequired,

    /**
     * Whether or not the component is in a disabled state.
     *
     * @type {Boolean}
     * @public
     */
    disabled: _propTypes["default"].bool,

    /**
     * The maximum value of ArcPicker.
     *
     * @type {Number}
     * @public
     */
    max: _propTypes["default"].number,

    /**
     * The min value of ArcPicker.
     *
     * @type {Number}
     * @public
     */
    min: _propTypes["default"].number,

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

    /**
     * Called when the path area is clicked.
     *
     * @type {Function}
     * @param {Object} event
     * @public
     */
    onClick: _propTypes["default"].func,

    /**
     * Value of ArcPicker.
     *
     * @type {Number|String}
     * @public
     */
    value: _propTypes["default"].oneOfType([_propTypes["default"].number, _propTypes["default"].string])
  }, _temp;
});
exports.ArcPickerBehaviorDecorator = ArcPickerBehaviorDecorator;
var _default = ArcPickerBehaviorDecorator;
exports["default"] = _default;