"use strict";

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Toggle = void 0;
var _react = _interopRequireWildcard(require("react"));
var _styledComponents = _interopRequireDefault(require("styled-components"));
var _ = require("../..");
var _utils = require("../../common/utils");
var _NormalizedElements = _interopRequireDefault(require("../../common/NormalizedElements"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
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(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
var TRACK_HEIGHT = {
  s: 4,
  m: 5,
  l: 4
};
var TRACK_WIDTH = {
  s: 7,
  m: 9,
  l: 10
};
var KNOB_SIZE = {
  s: 3,
  m: 4,
  l: 6
};
var Label = _styledComponents.default.label.withConfig({
  displayName: "Toggle__Label",
  componentId: "sc-12abobe-0"
})(["display:inline-block;cursor:", ";"], function (props) {
  return props.$disabled ? 'not-allowed' : 'pointer';
});
var Knob = _styledComponents.default.span.withConfig({
  displayName: "Toggle__Knob",
  componentId: "sc-12abobe-1"
})(["background:", ";display:block;height:", "px;width:", "px;position:absolute;left:", "px;border:1px solid ", ";margin-top:", "px;border-radius:", "px;box-sizing:border-box;box-shadow:0 1px 3px 1px ", ";transition:transform 0.2s cubic-bezier(0.18,0.9,0.35,1.15);cursor:", ";"], function (p) {
  return p.theme.color.toggleKnobEnabledOffBg;
}, function (p) {
  return p.theme.spacing.unit(KNOB_SIZE[p.$size]);
}, function (p) {
  return p.theme.spacing.unit(KNOB_SIZE[p.$size]);
}, function (p) {
  return p.$size === 'l' ? 0 : 2;
}, function (p) {
  return p.theme.color.inputBorder;
}, function (p) {
  return p.$size === 'l' ? -4 : 2;
}, function (p) {
  return p.theme.spacing.unit(KNOB_SIZE[p.$size] / 2);
}, function (p) {
  return p.theme.color.shadowSwitch;
}, function (props) {
  return props.$disabled ? 'not-allowed' : 'pointer';
});
var Track = _styledComponents.default.span.withConfig({
  displayName: "Toggle__Track",
  componentId: "sc-12abobe-2"
})(["display:block;height:", "px;width:", "px;margin:", "px 0;background-color:", ";border-radius:", "px;transition:background-color 0.2s ease-out;cursor:", ";"], function (p) {
  return p.theme.spacing.unit(TRACK_HEIGHT[p.$size]);
}, function (p) {
  return p.theme.spacing.unit(TRACK_WIDTH[p.$size]);
}, function (p) {
  return p.theme.spacing.unit((KNOB_SIZE[p.$size] - TRACK_HEIGHT[p.$size]) / 2);
}, function (p) {
  return p.theme.color.toggleTrackEnabledOffBg;
}, function (p) {
  return p.theme.spacing.unit(TRACK_HEIGHT[p.$size] / 2);
}, function (props) {
  return props.$disabled ? 'not-allowed' : 'pointer';
});
var ButtonContent = _styledComponents.default.div.withConfig({
  displayName: "Toggle__ButtonContent",
  componentId: "sc-12abobe-3"
})(["position:relative;"]);
var Button = (0, _styledComponents.default)(_NormalizedElements.default.Button).withConfig({
  displayName: "Toggle__Button",
  componentId: "sc-12abobe-4"
})(["display:block;background:none;padding:0;border:0;&[aria-checked='true']{", "{background-color:", ";}", "{transform:translate( ", "px );}}&[disabled],&[aria-readonly]{", "{background-color:", ";}", "{border:1px solid ", ";background-color:", ";}}&[aria-checked='true'][disabled]{", "{background-color:", ";}", "{border:1px solid ", ";background-color:", ";}}"], Track, function (p) {
  return p.theme.color.toggleTrackEnabledOnBg;
}, Knob, function (p) {
  return p.theme.spacing.unit(TRACK_WIDTH[p.$size] - KNOB_SIZE[p.$size] - (p.$size === 'l' ? 0 : 1));
}, Track, function (p) {
  return p.theme.color.toggleTrackDisabledOffBg;
}, Knob, function (p) {
  return p.theme.color.disabledBackground;
}, function (p) {
  return p.theme.color.toggleKnobDisabledOffBg;
}, Track, function (p) {
  return p.theme.color.toggleTrackDisabledOnBg;
}, Knob, function (p) {
  return p.theme.color.disabledBackground;
}, function (p) {
  return p.theme.color.toggleKnobDisabledOnBg;
});
var Toggle = exports.Toggle = function Toggle(_ref) {
  var className = _ref.className,
    label = _ref.label,
    hiddenLabel = _ref.hiddenLabel,
    disabled = _ref.disabled,
    onClick = _ref.onClick,
    _ref$checkedInitially = _ref.checkedInitially,
    checkedInitially = _ref$checkedInitially === void 0 ? false : _ref$checkedInitially,
    checkedControlled = _ref.checked,
    readOnly = _ref.readOnly,
    _ref$size = _ref.size,
    size = _ref$size === void 0 ? 'l' : _ref$size;
  var isControlled = (0, _utils.isBoolean)(checkedControlled);
  var titleNode = (0, _utils.isElement)(label) ? label : _react.default.createElement(_.Typography, {
    type: "secondary"
  }, label);
  var _useState = (0, _react.useState)(checkedInitially),
    _useState2 = _slicedToArray(_useState, 2),
    checkedInternal = _useState2[0],
    setCheckedInternal = _useState2[1];
  var checked = isControlled ? checkedControlled : checkedInternal;
  var internalClickHandler = function internalClickHandler(e) {
    var nextCheckedState = !checked;
    if (!readOnly) {
      setCheckedInternal(nextCheckedState);
    }
    if (onClick) {
      setTimeout(function () {
        return onClick(e, readOnly ? checked : nextCheckedState);
      });
    }
  };
  return _react.default.createElement(Label, {
    $disabled: disabled || hiddenLabel
  }, _react.default.createElement(_.Flexbox, {
    container: true,
    gap: 2,
    alignItems: "center",
    as: "span"
  }, hiddenLabel ? _react.default.createElement(_.VisuallyHidden, null, titleNode) : _react.default.createElement(_.Flexbox, {
    item: true,
    as: "span"
  }, titleNode), _react.default.createElement(_.Flexbox, {
    item: true,
    as: "span"
  }, _react.default.createElement(Button, {
    className: className,
    type: "button",
    role: "switch",
    "aria-checked": checked,
    onClick: isControlled ? onClick : internalClickHandler,
    disabled: disabled,
    "aria-readonly": readOnly,
    $size: size
  }, _react.default.createElement(ButtonContent, null, _react.default.createElement(Knob, {
    $size: size,
    $disabled: disabled
  }), _react.default.createElement(Track, {
    $size: size,
    $disabled: disabled
  }))))));
};