"use strict";

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

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

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

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

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

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

var _Spottable = _interopRequireDefault(require("@enact/spotlight/Spottable"));

var _Toggleable = _interopRequireDefault(require("@enact/ui/Toggleable"));

var _Icon = _interopRequireDefault(require("../Icon"));

var _Item = _interopRequireDefault(require("../Item"));

var _Skinnable = _interopRequireDefault(require("../Skinnable"));

var _Switch = require("../Switch");

var _SwitchItemModule = _interopRequireDefault(require("./SwitchItem.module.css"));

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

var Switch = (0, _Skinnable["default"])({
  prop: 'skin'
}, _Switch.SwitchBase);
/**
 * Renders an `Item` with a radio-dot component. Useful to show a selected state on an Item.
 *
 * @class SwitchItemBase
 * @memberof agate/SwitchItem
 * @ui
 * @public
 */

var SwitchItemBase = (0, _kind["default"])({
  name: 'SwitchItemBase',
  propTypes:
  /** @lends agate/SwitchItem.SwitchItemBase.prototype */
  {
    /**
     * Customizes the component by mapping the supplied collection of CSS class names to the
     * corresponding internal elements and states of this component.
     *
     * The following classes are supported:
     *
     * * `label` - Class name for the toggle icon label
     * * `switchIcon` - Class name for the toggle icon
     * * `switchItem` - The root class name
     *
     * @type {Object}
     * @public
     */
    css: _propTypes["default"].object,

    /**
     * Disables component and makes it non-interactive.
     *
     * @type {Boolean}
     * @default false
     * @public
     */
    disabled: _propTypes["default"].bool,

    /**
     * The icon to display before the text.
     *
     * @type {String|Object}
     * @see {@link agate/Icon.Icon}
     * @public
     */
    icon: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].object]),

    /**
     * Sets the switch to the 'on' state.
     *
     * @type {Boolean}
     * @default false
     * @public
     */
    selected: _propTypes["default"].bool,

    /**
     * Customize the text displayed when the switch is "off".
     *
     * @type {String}
     * @default 'off'
     * @public
     */
    switchOffLabel: _propTypes["default"].string,

    /**
     * Customize the text displayed when the switch is "on".
     *
     * @type {String}
     * @default 'on'
     * @public
     */
    switchOnLabel: _propTypes["default"].string
  },
  defaultProps: {
    disabled: false,
    selected: false,
    switchOffLabel: 'off',
    switchOnLabel: 'on'
  },
  styles: {
    css: _SwitchItemModule["default"],
    className: 'switchItem',
    publicClassNames: true
  },
  handlers: {
    onClick: (0, _handle["default"])((0, _handle.forProp)('disabled', false), (0, _handle.forward)('onClick'))
  },
  computed: {
    icon: function icon(_ref) {
      var css = _ref.css,
          _icon = _ref.icon;
      return _icon ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_Icon["default"], {
        size: "small",
        slot: "slotBefore",
        className: css.icon,
        children: _icon
      }) : null;
    }
  },
  render: function render(_ref2) {
    var children = _ref2.children,
        css = _ref2.css,
        icon = _ref2.icon,
        selected = _ref2.selected,
        switchOffLabel = _ref2.switchOffLabel,
        switchOnLabel = _ref2.switchOnLabel,
        rest = _objectWithoutProperties(_ref2, ["children", "css", "icon", "selected", "switchOffLabel", "switchOnLabel"]);

    return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_Item["default"], _objectSpread(_objectSpread({
      "aria-pressed": selected,
      role: "button"
    }, rest), {}, {
      css: css,
      label: selected ? switchOnLabel : switchOffLabel,
      labelPosition: "after",
      children: [icon, children, /*#__PURE__*/(0, _jsxRuntime.jsx)(Switch, {
        className: css.switchIcon,
        selected: selected,
        slot: "slotAfter"
      })]
    }));
  }
});
/**
 * Applies Agate specific behaviors to [SwitchItemBase]{@link agate/SwitchItem.SwitchItemBase} components.
 *
 * @hoc
 * @memberof agate/SwitchItem
 * @mixes ui/Toggleable.Toggleable
 * @mixes spotlight/Spottable.Spottable
 * @mixes agate/Skinnable.Skinnable
 * @public
 */

exports.SwitchItemBase = SwitchItemBase;
var SwitchItemDecorator = (0, _compose["default"])((0, _Toggleable["default"])({
  toggleProp: 'onClick'
}), // Touchable,
_Spottable["default"], _Skinnable["default"]);
/**
 * Renders an `Item` with a radio-dot component. Useful to show a selected state on an Item.
 *
 * @class SwitchItem
 * @memberof agate/SwitchItem
 * @extends agate/SwitchItem.SwitchItemBase
 * @mixes agate/SwitchItem.SwitchItemDecorator
 * @ui
 * @public
 */

exports.SwitchItemDecorator = SwitchItemDecorator;
var SwitchItem = SwitchItemDecorator(SwitchItemBase);
exports.SwitchItem = SwitchItem;
var _default = SwitchItem;
exports["default"] = _default;