"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = exports.KeypadDecorator = exports.KeypadBase = exports.Keypad = void 0;
var _handle = require("@enact/core/handle");
var _hoc = _interopRequireDefault(require("@enact/core/hoc"));
var _kind = _interopRequireDefault(require("@enact/core/kind"));
var _SpotlightContainerDecorator = require("@enact/spotlight/SpotlightContainerDecorator");
var _Layout = _interopRequireWildcard(require("@enact/ui/Layout"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _compose = _interopRequireDefault(require("ramda/src/compose"));
var _react = require("react");
var _$L = _interopRequireDefault(require("../internal/$L"));
var _Button = _interopRequireDefault(require("../Button"));
var _Skinnable = _interopRequireDefault(require("../Skinnable"));
var _KeypadModule = _interopRequireDefault(require("./Keypad.module.css"));
var _jsxRuntime = require("react/jsx-runtime");
var _excluded = ["children", "label", "text"],
  _excluded2 = ["disabled", "keys", "onKeyButtonClick", "spotlightDisabled"];
/* eslint-disable react/jsx-no-bind */
/**
 * Provides Agate-themed keypad components and behaviors. Used to display a sequence of numbers and buttons, like a keyboard.
 *
 * @example
 * <Keypad />
 *
 * @module agate/Keypad
 * @exports Keypad
 * @exports KeypadBase
 * @exports KeypadDecorator
 */
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == typeof e || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
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 _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; }
var SpotlightContainerLayout = (0, _SpotlightContainerDecorator.SpotlightContainerDecorator)({
  enterTo: 'default-element'
}, _Layout["default"]);

/**
 * Renders an Agate-styled Key button.
 *
 * @class Key
 * @memberof agate/Keypad
 * @ui
 * @public
 */
var Key = (0, _kind["default"])({
  name: 'Key',
  propTypes: /** @lends agate/Keypad.Key.prototype */{
    /**
     * The icon displayed on the key.
     *
     * @type {String}
     * @public
     */
    icon: _propTypes["default"].string,
    /**
     * Text displayed below the number/icon/symbol on the key.
     *
     * @type {String}
     * @public
     */
    label: _propTypes["default"].string,
    /**
     * Called when this button is clicked. Includes the 'key' key in its event payload to let the clicker know what was clicked inside their callback.
     *
     * @type {Function}
     * @param {Object} event
     * @public
     */
    onKeyButtonClick: _propTypes["default"].func,
    /**
     * Text displayed in the center of the key.
     *
     * @type {String}
     * @public
     */
    text: _propTypes["default"].string
  },
  styles: {
    css: _KeypadModule["default"],
    className: 'key'
  },
  handlers: {
    onClick: (0, _handle.handle)((0, _handle.forward)('onClick'), (0, _handle.adaptEvent)(function (ev, _ref) {
      var key = _ref.children;
      return {
        key: key
      };
    }, (0, _handle.forward)('onKeyButtonClick')))
  },
  render: function render(_ref2) {
    var children = _ref2.children,
      label = _ref2.label,
      text = _ref2.text,
      rest = _objectWithoutProperties(_ref2, _excluded);
    delete rest.onKeyButtonClick;
    return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: _KeypadModule["default"].keyContainer,
      children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_Button["default"], _objectSpread(_objectSpread({}, rest), {}, {
        css: _KeypadModule["default"],
        icon: children,
        marqueeDisabled: true,
        minWidth: false,
        role: null,
        size: "large",
        children: [text || text === 0 ? /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
          className: _KeypadModule["default"].text,
          children: text
        }) : null, label || label === 0 ? /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
          className: _KeypadModule["default"].label,
          children: label
        }) : null]
      }))
    });
  }
});

/**
 * The basic Layout of a Keypad component.
 *
 * @class KeypadBase
 * @memberof agate/Keypad
 * @extends agate/Keypad.Key
 * @ui
 * @public
 */
var KeypadBase = exports.KeypadBase = (0, _kind["default"])({
  name: 'Keypad',
  propTypes: /** @lends agate/Keypad.KeypadBase.prototype */{
    /**
     * Renders different icons depending on whether there is an active call or not.
     *
     * @type {Boolean}
     * @default false
     * @public
     */
    activeCall: _propTypes["default"].bool,
    /**
     * Applies a disabled style and the control becomes non-interactive.
     *
     * @type {Boolean}
     * @default false
     * @public
     */
    disabled: _propTypes["default"].bool,
    /**
     * Called when a button is clicked. Includes the 'key' key in its event payload and updates the state.
     *
     * @type {Function}
     * @param {Object} event
     * @public
     */
    onKeyButtonClick: _propTypes["default"].func,
    /**
     * The current skin for this component.
     *
     * @type {String}
     * @private
     */
    skin: _propTypes["default"].string,
    /**
     * Disables 5-way spotlight from navigating into the component.
     *
     * @type {Boolean}
     * @public
     */
    spotlightDisabled: _propTypes["default"].bool
  },
  styles: {
    css: _KeypadModule["default"],
    className: 'keypad'
  },
  computed: {
    keys: function keys(_ref3) {
      var activeCall = _ref3.activeCall,
        skin = _ref3.skin;
      var callStatus = activeCall ? 'calldecline' : 'callaccept';
      var editStatus = activeCall ? 'keypad' : 'backspace';
      return [{
        text: '1'
      }, {
        text: '2',
        label: 'abc'
      }, {
        text: '3',
        label: 'def'
      }, {
        text: '4',
        label: 'ghi'
      }, {
        text: '5',
        label: 'jkl'
      }, {
        text: '6',
        label: 'mno'
      }, {
        text: '7',
        label: 'pqrs'
      }, {
        text: '8',
        label: 'tuv'
      }, {
        text: '9',
        label: 'wxyz'
      }, {
        text: '*'
      }, {
        text: '0'
      }, {
        text: '#'
      }, {
        icon: skin !== 'silicon' ? 'phone' : callStatus
      }, {
        icon: skin !== 'silicon' ? 'arrowuturn' : editStatus
      }];
    }
  },
  render: function render(_ref4) {
    var disabled = _ref4.disabled,
      keys = _ref4.keys,
      _onKeyButtonClick = _ref4.onKeyButtonClick,
      spotlightDisabled = _ref4.spotlightDisabled,
      rest = _objectWithoutProperties(_ref4, _excluded2);
    delete rest.activeCall;
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(SpotlightContainerLayout, _objectSpread(_objectSpread({}, rest), {}, {
      align: "center end",
      className: _KeypadModule["default"].keypad,
      inline: true,
      spotlightDisabled: spotlightDisabled,
      wrap: true,
      children: keys.map(function (keyText, rowIndex) {
        var icon = keyText.icon,
          text = keyText.text;
        var ariaLabel = text;
        if (icon === 'arrowuturn') {
          ariaLabel = (0, _$L["default"])('backspace');
        } else if (icon === 'phone') {
          ariaLabel = (0, _$L["default"])('call');
        }
        return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Layout.Cell, {
          "aria-label": ariaLabel,
          className: _KeypadModule["default"][icon],
          component: Key,
          disabled: disabled,
          label: keyText.label,
          onKeyButtonClick: function onKeyButtonClick() {
            return _onKeyButtonClick(text || icon);
          },
          shrink: true,
          text: icon ? null : text,
          children: icon ? icon : null
        }, "key".concat(rowIndex, "-").concat(text));
      })
    }));
  }
});

/**
 * A Keypad component with specific functionality.
 *
 * @class KeypadBehaviorDecorator
 * @hoc
 * @memberof agate/Keypad
 * @private
 */
var KeypadBehaviorDecorator = (0, _hoc["default"])(function (config, Wrapped) {
  var _Class;
  return _Class = /*#__PURE__*/function (_Component) {
    function _Class(props) {
      var _this;
      _classCallCheck(this, _Class);
      _this = _callSuper(this, _Class, [props]);
      _this.handleKeypadValue = function (keyValue) {
        var _this$state = _this.state,
          charIndex = _this$state.charIndex,
          keypadValue = _this$state.keypadValue;
        var newKeypadValue = keypadValue;
        var newCharIndex;
        switch (keyValue) {
          case 'arrowuturn':
          case 'backspace':
            newCharIndex = charIndex;
            newKeypadValue = newKeypadValue.substring(0, charIndex - 1) + newKeypadValue.substring(charIndex, newKeypadValue.length);
            if (newCharIndex !== 0) {
              newCharIndex = newCharIndex - 1;
            }
            break;
          case 'phone':
          case 'callaccept':
          case 'calldecline':
          case 'keypad':
            newCharIndex = 0;
            newKeypadValue = '';
            break;
          default:
            newCharIndex = charIndex;
            newKeypadValue = newKeypadValue.substring(0, charIndex) + keyValue + newKeypadValue.substring(charIndex);
            newCharIndex = newCharIndex + 1;
            break;
        }
        if (keypadValue !== newKeypadValue) {
          (0, _handle.forward)('onChange', {
            value: newKeypadValue
          }, _this.props);
        }
        _this.setState({
          charIndex: newCharIndex,
          keypadValue: newKeypadValue
        });
      };
      _this.state = {
        charIndex: 0,
        keypadValue: ''
      };
      return _this;
    }
    _inherits(_Class, _Component);
    return _createClass(_Class, [{
      key: "render",
      value: function render() {
        return /*#__PURE__*/(0, _jsxRuntime.jsx)(Wrapped, _objectSpread(_objectSpread({}, this.props), {}, {
          onKeyButtonClick: this.handleKeypadValue
        }));
      }
    }]);
  }(_react.Component), _Class.displayName = 'KeypadBehaviorDecorator', _Class.propTypes = /** @lends agate/Keypad.KeypadBehaviorDecorator.prototype */{
    /**
     * Called when the value is changed.
     *
     * @type {Function}
     * @param {Object} event
     * @public
     */
    onChange: _propTypes["default"].func
  }, _Class;
});

/**
 * Applies Agate specific behaviors to {@link agate/Keypad.KeypadBase|KeypadBase}
 *
 * @hoc
 * @memberof agate/Keypad
 * @mixes agate/Skinnable.Skinnable
 * @public
 */
var KeypadDecorator = exports.KeypadDecorator = (0, _compose["default"])(KeypadBehaviorDecorator, (0, _Skinnable["default"])({
  prop: 'skin'
}));

/**
 * Provides Agate-themed keypad components and behaviors. Used to display a sequence of numbers and buttons, like a keyboard.
 *
 * @class Keypad
 * @memberof agate/Keypad
 * @extends agate/Keypad.KeypadBase
 * @public
 * @ui
 */
var Keypad = exports.Keypad = KeypadDecorator(KeypadBase);
var _default = exports["default"] = Keypad;