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; }
import React, { useState } from 'react';
import styled from 'styled-components';
import { Flexbox, Typography, VisuallyHidden } from '../..';
import { isBoolean, isElement } from '../../common/utils';
import NormalizedElements from '../../common/NormalizedElements';
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 = styled.label.withConfig({
  displayName: "Toggle__Label",
  componentId: "sc-12abobe-0"
})(["display:inline-block;cursor:", ";"], function (props) {
  return props.$disabled ? 'not-allowed' : 'pointer';
});
var Knob = styled.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 = styled.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 = styled.div.withConfig({
  displayName: "Toggle__ButtonContent",
  componentId: "sc-12abobe-3"
})(["position:relative;"]);
var Button = styled(NormalizedElements.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;
});
export var 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 = isBoolean(checkedControlled);
  var titleNode = isElement(label) ? label : React.createElement(Typography, {
    type: "secondary"
  }, label);
  var _useState = 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.createElement(Label, {
    $disabled: disabled || hiddenLabel
  }, React.createElement(Flexbox, {
    container: true,
    gap: 2,
    alignItems: "center",
    as: "span"
  }, hiddenLabel ? React.createElement(VisuallyHidden, null, titleNode) : React.createElement(Flexbox, {
    item: true,
    as: "span"
  }, titleNode), React.createElement(Flexbox, {
    item: true,
    as: "span"
  }, React.createElement(Button, {
    className: className,
    type: "button",
    role: "switch",
    "aria-checked": checked,
    onClick: isControlled ? onClick : internalClickHandler,
    disabled: disabled,
    "aria-readonly": readOnly,
    $size: size
  }, React.createElement(ButtonContent, null, React.createElement(Knob, {
    $size: size,
    $disabled: disabled
  }), React.createElement(Track, {
    $size: size,
    $disabled: disabled
  }))))));
};