"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.SwitchWithLocalState = SwitchWithLocalState;
exports["default"] = SwitchControlledByParent;

var _react = _interopRequireWildcard(require("react"));

var _SwitchModule = _interopRequireDefault(require("./Switch.module.css"));

var _getCompliedClassName5 = _interopRequireDefault(require("../../utils/getCompliedClassName"));

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

var _Switch2 = _interopRequireDefault(require("./Switch.constantProps"));

var _getCompliedCustomId = _interopRequireDefault(require("../../utils/getCompliedCustomId"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

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 getSwitchClassNames(props) {
  var _getCompliedClassName, _getCompliedClassName2, _getCompliedClassName3, _getCompliedClassName4;

  var size = props.size,
      color = props.color,
      customClassName = props.customClassName,
      label = props.label,
      shape = props.shape,
      disabled = props.disabled,
      labelPlacement = props.labelPlacement;
  var classNameForSwitch = (0, _getCompliedClassName5["default"])(customClassName, (_getCompliedClassName = {}, _defineProperty(_getCompliedClassName, _SwitchModule["default"]["switch"], true), _defineProperty(_getCompliedClassName, _SwitchModule["default"].labelplacementleft, !!(label && labelPlacement === 'left')), _defineProperty(_getCompliedClassName, _SwitchModule["default"].labelplacementtop, !!(label && labelPlacement === 'top')), _defineProperty(_getCompliedClassName, _SwitchModule["default"].labelplacementbottom, !!(label && labelPlacement === 'bottom')), _defineProperty(_getCompliedClassName, _SwitchModule["default"].disabled, !!disabled), _getCompliedClassName), (_getCompliedClassName2 = {}, _defineProperty(_getCompliedClassName2, customClassName["switch"], !!customClassName["switch"]), _defineProperty(_getCompliedClassName2, customClassName.labelplacementleft, !!(label && labelPlacement === 'left')), _defineProperty(_getCompliedClassName2, customClassName.labelplacementtop, !!(label && labelPlacement === 'top')), _defineProperty(_getCompliedClassName2, customClassName.labelplacementbottom, !!(label && labelPlacement === 'bottom')), _defineProperty(_getCompliedClassName2, customClassName.disabled, !!customClassName.disabled), _getCompliedClassName2));
  var classNameForSlider = (0, _getCompliedClassName5["default"])(customClassName, (_getCompliedClassName3 = {}, _defineProperty(_getCompliedClassName3, _SwitchModule["default"][shape], !!shape), _defineProperty(_getCompliedClassName3, _SwitchModule["default"][size], true), _defineProperty(_getCompliedClassName3, _SwitchModule["default"].slider, true), _defineProperty(_getCompliedClassName3, _SwitchModule["default"].disabled, !!disabled), _defineProperty(_getCompliedClassName3, _SwitchModule["default"][color], !!color), _defineProperty(_getCompliedClassName3, _SwitchModule["default"].sliderlabelplacementtop, !!(label && labelPlacement === 'top')), _getCompliedClassName3), (_getCompliedClassName4 = {}, _defineProperty(_getCompliedClassName4, customClassName[size], !!customClassName[size]), _defineProperty(_getCompliedClassName4, customClassName[color], !!customClassName[color]), _defineProperty(_getCompliedClassName4, customClassName.disabled, !!customClassName.disabled), _defineProperty(_getCompliedClassName4, customClassName.slider, !!customClassName.slider), _defineProperty(_getCompliedClassName4, customClassName.sliderlabelplacementtop, !!(label && labelPlacement === 'top')), _defineProperty(_getCompliedClassName4, customClassName[shape], !!customClassName[shape]), _getCompliedClassName4));
  var classNameForLabel = (0, _getCompliedClassName5["default"])(customClassName, {}, {});
  return {
    classNameForSwitch: classNameForSwitch,
    classNameForSlider: classNameForSlider,
    classNameForLabel: classNameForLabel
  };
}

function SwitchControlledByParent(props) {
  var checked = props.checked,
      onToggle = props.onToggle,
      customId = props.customId,
      disabled = props.disabled,
      label = props.label;
  var switchClassNameList = getSwitchClassNames(props);
  return /*#__PURE__*/_react["default"].createElement("label", {
    className: switchClassNameList.classNameForSwitch,
    tabIndex: disabled === true ? undefined : 0,
    "data-id": (0, _getCompliedCustomId["default"])(props, customId["switch"])
  }, /*#__PURE__*/_react["default"].createElement("input", {
    type: "checkbox",
    disabled: disabled,
    checked: checked,
    onChange: function onChange() {
      onToggle();
    }
  }), /*#__PURE__*/_react["default"].createElement("span", {
    className: switchClassNameList.classNameForSlider
  }), /*#__PURE__*/_react["default"].createElement("span", {
    className: switchClassNameList.classNameForLabel
  }, label));
}

SwitchControlledByParent.propTypes = _Switch["default"];
SwitchControlledByParent.defaultProps = _Switch.defaultProps;
SwitchControlledByParent.propsDescription = _Switch.propsDescription;
SwitchControlledByParent.constantProps = _Switch2["default"];

function SwitchWithLocalState(props) {
  var customId = props.customId,
      _onChange = props.onChange,
      disabled = props.disabled,
      label = props.label;

  var _useState = (0, _react.useState)(props.checked),
      _useState2 = _slicedToArray(_useState, 2),
      switchChecked = _useState2[0],
      setChecked = _useState2[1];

  var switchClassNameList = getSwitchClassNames(props);
  return /*#__PURE__*/_react["default"].createElement("label", {
    className: switchClassNameList.classNameForSwitch,
    tabIndex: disabled === true ? undefined : 0,
    "data-id": (0, _getCompliedCustomId["default"])(props, customId["switch"])
  }, /*#__PURE__*/_react["default"].createElement("input", {
    type: "checkbox",
    disabled: disabled,
    checked: switchChecked,
    onChange: function onChange() {
      setChecked(!switchChecked);
      _onChange && _onChange(!switchChecked);
    }
  }), /*#__PURE__*/_react["default"].createElement("span", {
    className: switchClassNameList.classNameForSlider
  }), /*#__PURE__*/_react["default"].createElement("span", {
    className: switchClassNameList.classNameForLabel
  }, label));
}

SwitchWithLocalState.propTypes = _Switch["default"];
SwitchWithLocalState.defaultProps = _Switch.defaultProps;
SwitchWithLocalState.propsDescription = _Switch.propsDescription;
SwitchWithLocalState.constantProps = _Switch2["default"];