"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Switch = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

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

var _Box = require("../Box");

var _styles = _interopRequireDefault(require("../Checkbox/styles"));

var _ControlBox = require("../ControlBox");

var _Form = require("../Form");

var _Text = require("../Text");

var _VisuallyHidden = require("../VisuallyHidden");

var _styles2 = _interopRequireDefault(require("./styles"));

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

var __jsx = _react["default"].createElement;
var Switch = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, ref) {
  var id = _ref.id,
      name = _ref.name,
      value = _ref.value,
      ariaLabel = _ref['aria-label'],
      ariaLabelledBy = _ref['aria-labelledby'],
      color = _ref.color,
      isChecked = _ref.isChecked,
      size = _ref.size,
      isDisabled = _ref.isDisabled,
      isInvalid = _ref.isInvalid,
      onChange = _ref.onChange,
      onBlur = _ref.onBlur,
      onFocus = _ref.onFocus,
      children = _ref.children,
      rest = (0, _objectWithoutProperties2["default"])(_ref, ["id", "name", "value", "aria-label", "aria-labelledby", "color", "isChecked", "size", "isDisabled", "isInvalid", "onChange", "onBlur", "onFocus", "children"]);

  var _useFormField = (0, _Form.useFormField)({
    name: name,
    onChange: onChange
  }),
      formOnChange = _useFormField.onChange,
      initialSwitchValue = _useFormField.value;

  var _rest$defaultIsChecke = rest.defaultIsChecked,
      defaultIsChecked = _rest$defaultIsChecke === void 0 ? initialSwitchValue : _rest$defaultIsChecke;

  var _useCheckboxStyle = (0, _styles["default"])({
    size: size
  }),
      labelStyleProps = _useCheckboxStyle.label,
      containerStyleProps = _useCheckboxStyle.container;

  var switchStyleProps = (0, _styles2["default"])({
    size: size,
    color: color
  });
  var height = switchStyleProps['height'];
  var rounded = switchStyleProps['rounded'] || 'full';

  var onSwitchChange = function onSwitchChange(v) {
    if (onChange) {
      onChange(v);
    }

    if (formOnChange && typeof formOnChange === 'function') {
      formOnChange(v, v.target.checked);
    }
  };

  return __jsx(_Box.Box, (0, _extends2["default"])({
    as: "label"
  }, containerStyleProps, rest), __jsx(_VisuallyHidden.VisuallyHidden, {
    as: "input",
    type: "checkbox",
    "aria-label": ariaLabel,
    "aria-labelledby": ariaLabelledBy,
    id: id,
    ref: ref,
    name: name,
    value: value,
    "aria-invalid": isInvalid,
    defaultChecked: defaultIsChecked,
    onChange: onSwitchChange,
    onBlur: onBlur,
    onFocus: onFocus,
    checked: isChecked,
    disabled: isDisabled
  }), __jsx(_ControlBox.ControlBox, switchStyleProps, __jsx(_Box.Box, {
    bg: "white",
    transition: "transform 250ms",
    rounded: rounded,
    size: height
  })), children && __jsx(_Text.Text, labelStyleProps, children));
});
exports.Switch = Switch;
//# sourceMappingURL=Switch.js.map