"use strict";

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

var _react = require("react");

var _bi = require("react-icons/bi");

var _bs = require("react-icons/bs");

var _indexModule = _interopRequireDefault(require("./index.module.css"));

var _jsxRuntime = require("react/jsx-runtime");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function TristateToggle(_ref) {
  var onChange = _ref.onChange,
      value = _ref.value,
      label = _ref.label,
      labelClass = _ref.labelClass,
      hideLabel = _ref.hideLabel,
      className = _ref.className;
  var states = [0, 1, -1];
  var colors = ["#e5e7eb", "#e05858", "#58b358"];
  var fgColors = ["#909090", "#fafafa", "#fafafa"];

  var _useState = (0, _react.useState)(value || states[0]),
      _useState2 = _slicedToArray(_useState, 2),
      stateValue = _useState2[0],
      setValue = _useState2[1];

  var _useState3 = (0, _react.useState)(undefined),
      _useState4 = _slicedToArray(_useState3, 2),
      position = _useState4[0],
      setPosition = _useState4[1];

  var _useState5 = (0, _react.useState)(colors[0]),
      _useState6 = _slicedToArray(_useState5, 2),
      bgColor = _useState6[0],
      setBgColor = _useState6[1];

  var _useState7 = (0, _react.useState)(fgColors[0]),
      _useState8 = _slicedToArray(_useState7, 2),
      fgColor = _useState8[0],
      setFgColor = _useState8[1];

  (0, _react.useEffect)(function () {
    updateButton(stateValue);

    if (value !== null) {
      setValue(value);
    } // eslint-disable-next-line react-hooks/exhaustive-deps

  }, [value]);
  (0, _react.useEffect)(function () {
    // trigger onChange callback on stateValue change
    onChange(stateValue);
    updateButton(stateValue); // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [stateValue]);

  function updateButton(stateValue) {
    // map and update position, bgColor, and tooltip text
    var pos;
    var colorIndex;

    switch (stateValue) {
      case -1:
        pos = "left";
        colorIndex = 1;
        break;

      case 1:
        pos = "right";
        colorIndex = 2;
        break;

      default:
        pos = undefined;
        colorIndex = 0;
    }

    setPosition(pos);
    setBgColor(colors[colorIndex]);
    setFgColor(fgColors[colorIndex]);
  }

  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: "".concat(className, " flex"),
    children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: "".concat(_indexModule.default.toggle),
      style: {
        backgroundColor: bgColor
      },
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("button", {
        type: "button",
        onClick: function onClick() {
          setValue(-1);
          updateButton(stateValue);
        },
        className: "".concat(_indexModule.default.button, " ").concat(_indexModule.default.buttonLeft),
        title: "Exclude ".concat(label.label),
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_bi.BiX, {
          style: {
            color: fgColor,
            marginLeft: "4px"
          }
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("button", {
        className: _indexModule.default.button,
        type: "button",
        title: "Do not filter ".concat(label.label),
        onClick: function onClick() {
          setValue(0);
          updateButton(stateValue);
        },
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_bs.BsDot, {
          style: {
            color: fgColor
          }
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("button", {
        type: "button",
        onClick: function onClick() {
          setValue(1);
          updateButton(stateValue);
        },
        className: "".concat(_indexModule.default.button, " ").concat(_indexModule.default.buttonLeft),
        title: "Include ".concat(label.label),
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_bi.BiCheck, {
          style: {
            color: fgColor,
            marginRight: "4px"
          }
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: "".concat(_indexModule.default.buttonInnerCircle, "  ").concat(position && _indexModule.default[position])
      })]
    }), states.map(function (state) {
      return /*#__PURE__*/(0, _jsxRuntime.jsx)("input", {
        name: label.id,
        onChange: onChange,
        className: "hidden",
        type: "radio",
        value: stateValue,
        checked: state === stateValue
      }, state);
    }), label.label && !hideLabel && /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
      className: "".concat(labelClass || "ml-3 text-xs text-left text-gray-700"),
      children: label.label
    })]
  });
}