"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = exports.ToggleButtonDecorator = exports.ToggleButtonBase = exports.ToggleButton = void 0;
var _kind = _interopRequireDefault(require("@enact/core/kind"));
var _Pure = _interopRequireDefault(require("@enact/ui/internal/Pure"));
var _Toggleable = _interopRequireDefault(require("@enact/ui/Toggleable"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _compose = _interopRequireDefault(require("ramda/src/compose"));
var _Button = _interopRequireDefault(require("../Button"));
var _Skinnable = _interopRequireDefault(require("../Skinnable"));
var _ToggleButtonModule = _interopRequireDefault(require("./ToggleButton.module.css"));
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": 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); } /**
 * Agate styled ToggleButton components and behaviors.
 *
 * @example
 * <ToggleButton underline>Hello Enact!</ToggleButton>
 *
 * @module agate/ToggleButton
 * @exports ToggleButton
 * @exports ToggleButtonBase
 * @exports ToggleButtonDecorator
 */ /**
 * A stateless {@link agate/Button.Button|Button} that can be toggled by changing its
 * `selected` property.
 *
 * @class ToggleButtonBase
 * @memberof agate/ToggleButton
 * @extends agate/Button.Button
 * @ui
 * @public
 */
var ToggleButtonBase = exports.ToggleButtonBase = (0, _kind["default"])({
  name: 'ToggleButton',
  propTypes: /** @lends agate/ToggleButton.ToggleButtonBase.prototype */{
    /**
     * The string to be displayed as the main content of the toggle button.
     *
     * If `toggleOffLabel` and/or `toggleOnLabel` are provided, they will be used for the
     * respective states.
     *
     * @type {Node}
     * @public
     */
    children: _propTypes["default"].node,
    /**
     * Disables the button.
     *
     * @type {Boolean}
     * @default false
     * @public
     */
    disabled: _propTypes["default"].bool,
    /**
     * The icon to display before the text.
     *
     * @type {String}
     * @see {@link agate/Icon.Icon}
     */
    icon: _propTypes["default"].string,
    /**
     * Determines whether the button is currently in an "on" or "off" state.
     *
     * @type {Boolean}
     * @default false
     * @public
     */
    selected: _propTypes["default"].bool,
    /**
     * The size of the button.
     *
     * @type {('smallest'|'small'|'large'|'huge')}
     * @default 'large'
     * @public
     */
    size: _propTypes["default"].oneOf(['smallest', 'small', 'large', 'huge']),
    /**
     * Button text displayed in the 'off' state.
     *
     * If not specified, `children` will be used for 'off' button text.
     *
     * @type {String}
     * @default ''
     * @public
     */
    toggleOffLabel: _propTypes["default"].string,
    /**
     * Button text displayed in the 'on' state.
     *
     * If not specified, `children` will be used for 'on' button text.
     *
     * @type {String}
     * @default ''
     * @public
     */
    toggleOnLabel: _propTypes["default"].string,
    /**
     * The button type.
     *
     * Grid buttons are intended to be grouped with other related buttons.
     *
     * @type {('grid'|'standard')}
     * @public
     */
    type: _propTypes["default"].oneOf(['grid', 'standard']),
    /**
     * Shows toggle indicator.
     *
     * @type {Boolean}
     * @default false
     * @public
     */
    underline: _propTypes["default"].bool
  },
  defaultProps: {
    disabled: false,
    selected: false,
    size: 'large',
    toggleOffLabel: '',
    toggleOnLabel: '',
    underline: false
  },
  styles: {
    css: _ToggleButtonModule["default"],
    className: 'toggleButton',
    publicClassNames: true
  },
  computed: {
    className: function className(_ref) {
      var selected = _ref.selected,
        styler = _ref.styler,
        underline = _ref.underline;
      return styler.append({
        selected: selected,
        underline: underline
      });
    },
    children: function children(_ref2) {
      var _children = _ref2.children,
        selected = _ref2.selected,
        toggleOnLabel = _ref2.toggleOnLabel,
        toggleOffLabel = _ref2.toggleOffLabel;
      var c = _children;
      if (selected && toggleOnLabel) {
        c = toggleOnLabel;
      } else if (!selected && toggleOffLabel) {
        c = toggleOffLabel;
      }
      return c;
    }
  },
  render: function render(props) {
    delete props.underline;
    delete props.toggleOffLabel;
    delete props.toggleOnLabel;
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Button["default"], _objectSpread(_objectSpread({}, props), {}, {
      "aria-pressed": props.selected,
      css: _ToggleButtonModule["default"],
      minWidth: false
    }));
  }
});

/**
 * Applies Agate specific behaviors to {@link agate/ToggleButton.ToggleButtonBase|ToggleButtonBase} components.
 *
 * @hoc
 * @memberof agate/ToggleButton
 * @mixes ui/Toggleable.Toggleable
 * @mixes agate/Skinnable.Skinnable
 * @public
 */
var ToggleButtonDecorator = exports.ToggleButtonDecorator = (0, _compose["default"])(_Pure["default"], (0, _Toggleable["default"])({
  prop: 'selected',
  toggleProp: 'onTap'
}), _Skinnable["default"]);

/**
 * A toggleable button.
 *
 * By default, `ToggleButton` maintains the state of its `selected` property.
 * Supply the `defaultSelected` property to control its initial value. If you
 * wish to directly control updates to the component, supply a value to `selected` at creation time
 * and update it in response to `onToggle` events.
 *
 * @class ToggleButton
 * @memberof agate/ToggleButton
 * @extends agate/ToggleButton.ToggleButtonBase
 * @mixes agate/ToggleButton.ToggleButtonDecorator
 * @ui
 * @public
 */
var ToggleButton = exports.ToggleButton = ToggleButtonDecorator(ToggleButtonBase);
var _default = exports["default"] = ToggleButton;