"use strict";

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

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

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

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

/**
 * A stateless [Button]{@link agate/Button.Button} that can be toggled by changing its
 * `selected` property.
 *
 * @class ToggleButtonBase
 * @memberof agate/ToggleButton
 * @extends agate/Button.Button
 * @ui
 * @public
 */
var 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 [ToggleButtonBase]{@link agate/ToggleButton.ToggleButtonBase} components.
 *
 * @hoc
 * @memberof agate/ToggleButton
 * @mixes ui/Toggleable.Toggleable
 * @mixes agate/Skinnable.Skinnable
 * @public
 */

exports.ToggleButtonBase = ToggleButtonBase;
var 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
 */

exports.ToggleButtonDecorator = ToggleButtonDecorator;
var ToggleButton = ToggleButtonDecorator(ToggleButtonBase);
exports.ToggleButton = ToggleButton;
var _default = ToggleButton;
exports["default"] = _default;