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 TOGGLE_HEIGHT = 5;
var TRACK_HEIGHT = TOGGLE_HEIGHT;
var TRACK_WIDTH = 17.75;
var TRACK_PADDING = 0.5;
var TRACK_TO_KNOW_RATIO = 1.775;
var KNOB_HEIGHT = TOGGLE_HEIGHT;
var KNOB_WIDTH = TRACK_WIDTH / TRACK_TO_KNOW_RATIO;
var Label = styled.label.withConfig({
  displayName: "SwitchToggle__Label",
  componentId: "sc-fgo1xn-0"
})(["display:inline-block;"]);
var Knob = styled(Flexbox).withConfig({
  displayName: "SwitchToggle__Knob",
  componentId: "sc-fgo1xn-1"
})(["height:", "px;width:", "px;position:absolute;padding-top:1px;top:", "px;left:", "px;border-radius:", "px;box-sizing:border-box;transition:transform 0.6s cubic-bezier(0.18,0.9,0.35,1.15);background-color:", ";"], function (p) {
  return p.theme.spacing.unit(KNOB_HEIGHT);
}, function (p) {
  if (p.knobwidth) {
    return p.theme.spacing.unit(p.knobwidth);
  }
  return p.theme.spacing.unit(KNOB_WIDTH);
}, function (p) {
  return p.theme.spacing.unit(TRACK_PADDING);
}, function (p) {
  return p.theme.spacing.unit(TRACK_PADDING);
}, function (p) {
  return p.theme.spacing.unit(TRACK_HEIGHT);
}, function (p) {
  return p.theme.color.bubbleBackground;
});
var Track = styled.span.withConfig({
  displayName: "SwitchToggle__Track",
  componentId: "sc-fgo1xn-2"
})(["display:block;height:", "px;width:", "px;padding:", "px ", "px;border-radius:", "px;line-height:", "px;background-color:", ";box-shadow:0 1px 3px 1px ", ";"], function (p) {
  return p.theme.spacing.unit(TRACK_HEIGHT);
}, function (p) {
  return p.width ? p.theme.spacing.unit(p.width) : p.theme.spacing.unit(TRACK_WIDTH);
}, function (p) {
  return p.theme.spacing.unit(TRACK_PADDING);
}, function (p) {
  return p.theme.spacing.unit(TRACK_PADDING * 2);
}, function (p) {
  return p.theme.spacing.unit(TRACK_HEIGHT);
}, function (p) {
  return p.theme.spacing.unit(TOGGLE_HEIGHT);
}, function (p) {
  return p.theme.color.background;
}, function (p) {
  return p.theme.color.background;
});
var ButtonContent = styled.div.withConfig({
  displayName: "SwitchToggle__ButtonContent",
  componentId: "sc-fgo1xn-3"
})(["position:relative;"]);
var Button = styled(NormalizedElements.Button).withConfig({
  displayName: "SwitchToggle__Button",
  componentId: "sc-fgo1xn-4"
})(["display:block;background:none;padding:0;border:0;&[aria-checked='true']{", "{transform:translate( ", "px );}}&[disabled],&[aria-readonly]{", "{background-color:", ";}", "{background-color:", ";}}&[aria-checked='true'][aria-readonly]{", "{background-color:", ";}}"], Knob, function (p) {
  if (p.knobwidth && p.width) {
    return p.theme.spacing.unit(p.width - p.knobwidth + 1);
  }
  return p.theme.spacing.unit(TRACK_WIDTH - KNOB_WIDTH + 1);
}, Track, function (p) {
  return p.theme.color.disabledBackground;
}, Knob, function (p) {
  return p.theme.color.switchReadOnlyKnobBg;
}, Track, function (p) {
  return p.theme.color.switchReadOnlyTrackBg;
});
var StyledTypography = styled(Typography).withConfig({
  displayName: "SwitchToggle__StyledTypography",
  componentId: "sc-fgo1xn-5"
})(["width:", "px;"], function (p) {
  if (p.width) {
    return p.theme.spacing.unit(p.width / 2);
  }
  return p.theme.spacing.unit(TRACK_WIDTH / 2);
});
export var SwitchToggle = function SwitchToggle(_ref) {
  var className = _ref.className,
    label = _ref.label,
    hiddenLabel = _ref.hiddenLabel,
    valueLeft = _ref.valueLeft,
    valueRight = _ref.valueRight,
    disabled = _ref.disabled,
    onClick = _ref.onClick,
    _ref$checkedInitially = _ref.checkedInitially,
    checkedInitially = _ref$checkedInitially === void 0 ? false : _ref$checkedInitially,
    checkedControlled = _ref.checked,
    readOnly = _ref.readOnly,
    width = _ref.width;
  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 knobwidth = width ? width / TRACK_TO_KNOW_RATIO : KNOB_WIDTH;
  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, null, React.createElement(Flexbox, {
    container: true,
    gap: 2,
    alignItems: "center",
    as: "span"
  }, React.createElement(Flexbox, {
    item: true,
    as: "span"
  }, hiddenLabel ? React.createElement(VisuallyHidden, null, titleNode) : 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,
    knobwidth: knobwidth,
    width: width
  }, React.createElement(ButtonContent, null, React.createElement(Knob, {
    knobwidth: knobwidth,
    container: true,
    item: true,
    justifyContent: "center",
    alignItems: "center",
    alignContent: "center"
  }, React.createElement(Typography, {
    type: "secondary",
    weight: "bold",
    color: function color(p) {
      return p.color.cta;
    }
  }, checked ? valueRight : valueLeft)), React.createElement(Track, {
    width: width
  }, React.createElement(Flexbox, {
    container: true,
    item: true,
    justifyContent: "space-between",
    alignItems: "center",
    alignContent: "center"
  }, React.createElement(StyledTypography, {
    type: "secondary",
    width: width,
    color: function color(p) {
      return p.color.disabledText;
    }
  }, valueLeft), React.createElement(StyledTypography, {
    type: "secondary",
    width: width,
    color: function color(p) {
      return p.color.disabledText;
    }
  }, valueRight))))))));
};