"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ChooseV2 = void 0;
var _react = _interopRequireWildcard(require("react"));
var _Styles = require("./Styles");
var _utils = require("../../utils");
var _ButtonGroup = require("../ButtonGroup");
var _Checkbox = require("../Checkbox");
var _RadioButton = require("../RadioButton");
var _excluded = ["required", "multiple", "invalid", "options", "selected", "onChange"];
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 _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
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; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
var ChooseV2 = exports.ChooseV2 = /*#__PURE__*/_react.default.forwardRef(function (_ref, ref) {
  var _parentSize$width, _flexSize$width;
  var _ref$required = _ref.required,
    required = _ref$required === void 0 ? false : _ref$required,
    _ref$multiple = _ref.multiple,
    multiple = _ref$multiple === void 0 ? false : _ref$multiple,
    _ref$invalid = _ref.invalid,
    invalid = _ref$invalid === void 0 ? false : _ref$invalid,
    options = _ref.options,
    selected = _ref.selected,
    onChange = _ref.onChange,
    rest = _objectWithoutProperties(_ref, _excluded);
  var internalRef = (0, _react.useRef)(null);
  var flexContainerRef = (0, _react.useRef)(null);
  var handleRef = (0, _utils.useForkRef)(ref, internalRef);
  var _useState = (0, _react.useState)(false),
    _useState2 = _slicedToArray(_useState, 2),
    useVerticalLayout = _useState2[0],
    setUseVerticalLayout = _useState2[1];
  var _useState3 = (0, _react.useState)(false),
    _useState4 = _slicedToArray(_useState3, 2),
    isReady = _useState4[0],
    setIsReady = _useState4[1];
  var parentSize = (0, _utils.useResizeObserver)(internalRef);
  var flexSize = (0, _utils.useResizeObserver)(flexContainerRef);
  var parentWidth = (_parentSize$width = parentSize === null || parentSize === void 0 ? void 0 : parentSize.width) !== null && _parentSize$width !== void 0 ? _parentSize$width : 0;
  var flexWidth = (_flexSize$width = flexSize === null || flexSize === void 0 ? void 0 : flexSize.width) !== null && _flexSize$width !== void 0 ? _flexSize$width : 0;
  (0, _react.useEffect)(function () {
    if (parentWidth === 0) return;
    var measure = function measure() {
      var temp = document.createElement("div");
      temp.style.display = "inline-flex";
      temp.style.visibility = "hidden";
      temp.style.position = "absolute";
      temp.style.whiteSpace = "nowrap";
      temp.style.pointerEvents = "none";
      document.body.appendChild(temp);
      options.forEach(function (option) {
        var button = document.createElement("button");
        button.textContent = option.name;
        button.style.padding = "6px 12px";
        button.style.border = "1px solid transparent";
        button.style.marginRight = "4px";
        temp.appendChild(button);
      });
      var totalWidth = temp.offsetWidth + 8;
      document.body.removeChild(temp);
      setUseVerticalLayout(totalWidth > parentWidth);
      setIsReady(true);
    };
    requestAnimationFrame(measure);
  }, [parentWidth, options, flexWidth]);
  var handleSelect = function handleSelect(id) {
    if (multiple) {
      var newSelected = selected.includes(id) ? selected.filter(function (s) {
        return s !== id;
      }) : [...selected, id];
      if (required && newSelected.length === 0) return;
      onChange(newSelected);
    } else {
      var _newSelected = selected.includes(id) ? [] : [id];
      if (required && _newSelected.length === 0) return;
      onChange(_newSelected);
    }
  };
  if (!isReady) {
    return /*#__PURE__*/_react.default.createElement("div", {
      ref: handleRef,
      style: {
        width: "100%",
        minHeight: "32px"
      }
    });
  }
  return /*#__PURE__*/_react.default.createElement("div", _extends({
    ref: handleRef
  }, rest, {
    style: {
      display: "flex",
      width: "100%"
    }
  }), useVerticalLayout ? /*#__PURE__*/_react.default.createElement(_Styles.StyledVerticalContainer, null, options.map(function (option) {
    return /*#__PURE__*/_react.default.createElement(_Styles.StyledRadioOption, {
      $disabled: !!option.disabled,
      key: option.id,
      "data-disabled": option.disabled
    }, multiple || !required ? /*#__PURE__*/_react.default.createElement(_Checkbox.Checkbox, {
      checked: selected.includes(option.id),
      disabled: option.disabled,
      onChange: function onChange() {
        return !option.disabled && handleSelect(option.id);
      },
      name: "choose-v2-".concat(option.id),
      id: "choose-v2-".concat(option.id)
    }) : /*#__PURE__*/_react.default.createElement(_RadioButton.RadioButton, {
      checked: selected.includes(option.id),
      disabled: option.disabled,
      onChange: function onChange() {
        return !option.disabled && handleSelect(option.id);
      },
      id: "choose-v2-".concat(option.id),
      name: "choose-v2-".concat(option.id)
    }), /*#__PURE__*/_react.default.createElement(_Styles.StyledLabel, {
      $disabled: !!option.disabled,
      weight: "regular",
      htmlFor: "choose-v2-".concat(option.id)
    }, option.name));
  })) : /*#__PURE__*/_react.default.createElement(_Styles.StyledFlexContainer, {
    ref: flexContainerRef
  }, /*#__PURE__*/_react.default.createElement(_ButtonGroup.ButtonGroup, {
    invalid: invalid
  }, options.map(function (option) {
    return /*#__PURE__*/_react.default.createElement(_Styles.StyledChoose, {
      key: option.id,
      active: selected.includes(option.id),
      disabled: option.disabled,
      onClick: function onClick() {
        return handleSelect(option.id);
      }
    }, option.name);
  }))));
});
ChooseV2.displayName = "ChooseV2";
//# sourceMappingURL=ChooseV2.js.map