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 = 7 - 1;
var TRACK_HEIGHT = TOGGLE_HEIGHT;
var TRACK_WIDTH = 22.5 - 1;
var TRACK_PADDING = 0.5;
var TRACK_TO_KNOB_RATIO = 2;
var KNOB_HEIGHT = TOGGLE_HEIGHT;
var KNOB_WIDTH = TRACK_WIDTH / TRACK_TO_KNOB_RATIO;
var Label = styled.label.withConfig({
  displayName: "DataToggle__Label",
  componentId: "sc-excuo3-0"
})(["display:inline-block;"]);
var Knob = styled(Flexbox).withConfig({
  displayName: "DataToggle__Knob",
  componentId: "sc-excuo3-1"
})(["height:", "px;width:", "px;border-radius:", "px;box-sizing:border-box;transition:transform 0.6s cubic-bezier(0.18,0.9,0.35,1.15);background-color:", ";box-shadow:0 2px 2px rgb(0 0 0 / 3%);"], 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_HEIGHT);
}, function (p) {
  return p.theme.color.bubbleBackground;
});
var Track = styled.span.withConfig({
  displayName: "DataToggle__Track",
  componentId: "sc-excuo3-2"
})(["display:block;position:relative;height:", "px;width:", "px;padding:", "px;border-radius:", "px;line-height:", "px;background-color:", ";"], 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_HEIGHT);
}, function (p) {
  return p.theme.spacing.unit(TOGGLE_HEIGHT);
}, function (p) {
  return p.theme.color.background;
});
var InnerTrack = styled(Flexbox).withConfig({
  displayName: "DataToggle__InnerTrack",
  componentId: "sc-excuo3-3"
})(["position:absolute;z-index:2;top:0;height:", "px;"], function (p) {
  return p.theme.spacing.unit(TRACK_HEIGHT + 1);
});
var ButtonContent = styled.div.withConfig({
  displayName: "DataToggle__ButtonContent",
  componentId: "sc-excuo3-4"
})(["position:relative;"]);
var Button = styled(NormalizedElements.Button).withConfig({
  displayName: "DataToggle__Button",
  componentId: "sc-excuo3-5"
})(["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);
  }
  return p.theme.spacing.unit(TRACK_WIDTH - KNOB_WIDTH);
}, 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: "DataToggle__StyledTypography",
  componentId: "sc-excuo3-6"
})(["padding-top:1px;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 DataToggle = function DataToggle(_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 adjustWidthForXPaddings = width ? width - 1 : undefined;
  var knobwidth = width ? width / TRACK_TO_KNOB_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,
    alignItems: "center",
    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: adjustWidthForXPaddings
  }, React.createElement(ButtonContent, null, React.createElement(Track, {
    width: adjustWidthForXPaddings
  }, React.createElement(Knob, {
    knobwidth: knobwidth,
    container: true,
    item: true,
    justifyContent: "center",
    alignItems: "center",
    alignContent: "center"
  }), React.createElement(InnerTrack, {
    container: true,
    item: true,
    justifyContent: "space-between",
    alignItems: "center",
    alignContent: "center"
  }, React.createElement(StyledTypography, {
    type: "secondary",
    weight: "bold",
    width: adjustWidthForXPaddings,
    color: !checked ? function (p) {
      return p.color.cta;
    } : function (p) {
      return p.color.disabledText;
    }
  }, valueLeft), React.createElement(StyledTypography, {
    type: "secondary",
    weight: "bold",
    width: adjustWidthForXPaddings,
    color: checked ? function (p) {
      return p.color.cta;
    } : function (p) {
      return p.color.disabledText;
    }
  }, valueRight))))))));
};