"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
Object.defineProperty(exports, "components", {
  enumerable: true,
  get: function get() {
    return _reactSelect.components;
  }
});
exports.Select = void 0;

var _react = _interopRequireDefault(require("react"));

var _reactSelect = _interopRequireWildcard(require("react-select"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _reactHookForm = require("react-hook-form");

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

var _excluded = ["children"];

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread 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 _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }

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

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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

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 SelectAllFeatureValueContainer = function SelectAllFeatureValueContainer(_ref) {
  var children = _ref.children,
      props = _objectWithoutProperties(_ref, _excluded);

  var currentValues = props.getValue();
  var toBeRendered = children;

  if (currentValues.some(function (val) {
    var _props$selectProps$al;

    return val.value === ((_props$selectProps$al = props.selectProps.allOption) === null || _props$selectProps$al === void 0 ? void 0 : _props$selectProps$al.value);
  })) {
    toBeRendered = _react.default.Children.toArray(children).filter(function (c) {
      var _props$selectProps$al2;

      return !c.props.data || c.props.data.value !== ((_props$selectProps$al2 = props.selectProps.allOption) === null || _props$selectProps$al2 === void 0 ? void 0 : _props$selectProps$al2.value);
    });
  }

  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactSelect.components.ValueContainer, _objectSpread(_objectSpread({}, props), {}, {
    children: toBeRendered
  }));
};

var selectAllOptionFeatureDecorator = function selectAllOptionFeatureDecorator(originalProps) {
  var allowSelectAll = originalProps.allowSelectAll,
      allOption = originalProps.allOption,
      initialOnChange = originalProps.onChange,
      options = originalProps.options,
      value = originalProps.value,
      customValueContainer = originalProps.customValueContainer;

  if (!allowSelectAll) {
    return {};
  }

  var isAllOptionSelected = (value === null || value === void 0 ? void 0 : value.length) === options.length;
  var fullOptions = [allOption].concat(_toConsumableArray(options));

  var onChange = function onChange(newValue, actionMeta) {
    var action = actionMeta.action,
        option = actionMeta.option,
        removedValue = actionMeta.removedValue;

    switch (action) {
      case "select-option":
        {
          if (option.value === allOption.value) {
            return initialOnChange(options, actionMeta);
          }

          return initialOnChange(newValue || [], actionMeta);
        }

      case "deselect-option":
        {
          if (option.value === allOption.value) {
            return initialOnChange([], actionMeta);
          }

          if (isAllOptionSelected) {
            return initialOnChange(options.filter(function (_ref2) {
              var value = _ref2.value;
              return value !== option.value;
            }), actionMeta);
          }

          return initialOnChange(newValue || [], actionMeta);
        }

      case "remove-value":
        {
          if (removedValue.value === allOption.value) {
            return initialOnChange([], actionMeta);
          }

          if (isAllOptionSelected) {
            return initialOnChange(options.filter(function (_ref3) {
              var value = _ref3.value;
              return value !== removedValue.value;
            }), actionMeta);
          }

          return initialOnChange(newValue || [], actionMeta);
        }

      default:
        return initialOnChange(newValue || [], actionMeta);
    }
  };

  return {
    onChange: onChange,
    options: fullOptions,
    value: isAllOptionSelected ? [allOption].concat(_toConsumableArray(value)) : value,
    components: _objectSpread(_objectSpread({}, originalProps.components), {}, {
      ValueContainer: customValueContainer || SelectAllFeatureValueContainer
    })
  };
};

var selectColourStyles = {
  control: function control(styles, _ref4) {
    var isFocused = _ref4.isFocused;
    return _objectSpread(_objectSpread({}, styles), {}, {
      minHeight: "42px",
      border: "1px solid #D1D5DB",
      borderColor: isFocused ? "#6366F1" : undefined,
      boxShadow: isFocused ? "0px 0px 0px 2px rgba(99, 102, 241, 1), 0px 1px 2px 0px rgba(0, 0, 0, 0.05)" : "0px 1px 2px rgba(0, 0, 0, 0.05)",
      borderRadius: "6px",
      "&:hover": _objectSpread(_objectSpread({}, styles["&:hover"]), {}, {
        borderColor: undefined,
        boxShadow: undefined
      })
    });
  },
  option: function option(styles, _ref5) {
    var isSelected = _ref5.isSelected,
        isFocused = _ref5.isFocused;
    return _objectSpread(_objectSpread({}, styles), {}, {
      backgroundColor: isSelected ? "#4e45df" : isFocused ? "#e6e4fa" : undefined,
      ":active": _objectSpread(_objectSpread({}, styles[":active"]), {}, {
        backgroundColor: "#ccc9f6"
      })
    });
  },
  indicatorSeparator: function indicatorSeparator(styles) {
    return _objectSpread(_objectSpread({}, styles), {}, {
      backgroundColor: "#9CA3AF"
    });
  },
  dropdownIndicator: function dropdownIndicator(styles) {
    return _objectSpread(_objectSpread({}, styles), {}, {
      color: "#9CA3AF"
    });
  },
  clearIndicator: function clearIndicator(styles) {
    return _objectSpread(_objectSpread({}, styles), {}, {
      color: "#9CA3AF"
    });
  },
  menu: function menu(styles) {
    return _objectSpread(_objectSpread({}, styles), {}, {
      borderRadius: "6px",
      boxShadow: "0px 10px 15px -3px rgba(0, 0, 0, 0.1), 0px 4px 6px -2px rgba(0, 0, 0, 0.05), 0px 0px 0px 1px rgba(0, 0, 0, 0.05)"
    });
  },
  singleValue: function singleValue(styles) {
    return _objectSpread(_objectSpread({}, styles), {}, {
      fontSize: "16px",
      lineHeight: "24px"
    });
  },
  input: function input(styles) {
    return _objectSpread(_objectSpread({}, styles), {}, {
      "& input:focus": {
        boxShadow: "none"
      }
    });
  }
};

var Select = function Select(props) {
  var name = props.name,
      control = props.control,
      styles = props.styles,
      customValueContainer = props.customValueContainer;

  if (control) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactHookForm.Controller, {
      control: control,
      name: name,
      render: function render(_ref6) {
        var _ref6$field = _ref6.field,
            onChange = _ref6$field.onChange,
            value = _ref6$field.value,
            ref = _ref6$field.ref,
            name = _ref6$field.name;

        var composedProps = _objectSpread(_objectSpread({}, props), {}, {
          inputRef: ref,
          name: name,
          value: props.isMulti ? props.options.filter(function (o) {
            return value === null || value === void 0 ? void 0 : value.includes(o.value);
          }) : props.options.find(function (o) {
            return o.value === value;
          }),
          onChange: props.isMulti ? function (value) {
            return onChange(value.map(function (i) {
              return i.value;
            }));
          } : function (value) {
            return onChange(value === null || value === void 0 ? void 0 : value.value);
          },
          styles: _objectSpread(_objectSpread({}, selectColourStyles), props.styles || {}),
          customValueContainer: customValueContainer
        });

        var selectAllProps = selectAllOptionFeatureDecorator(composedProps);
        return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactSelect.default, _objectSpread(_objectSpread({}, composedProps), selectAllProps));
      }
    });
  }

  var composedProps = _objectSpread(_objectSpread({}, props), {}, {
    styles: _objectSpread(_objectSpread({}, selectColourStyles), styles || {})
  });

  var selectAllProps = selectAllOptionFeatureDecorator(composedProps);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactSelect.default, _objectSpread(_objectSpread({}, composedProps), selectAllProps));
};

exports.Select = Select;
Select.propTypes = {
  // eslint-disable-next-line react/require-default-props
  options: _propTypes.default.arrayOf(_propTypes.default.any),
  // eslint-disable-next-line react/forbid-prop-types,react/require-default-props
  allowSelectAll: _propTypes.default.bool,
  allOption: _propTypes.default.shape({
    label: _propTypes.default.string,
    value: _propTypes.default.string
  }),
  control: _propTypes.default.shape({}),
  name: _propTypes.default.string.isRequired,
  onChange: _propTypes.default.func,
  // eslint-disable-next-line react/forbid-prop-types
  value: _propTypes.default.any
};
Select.defaultProps = {
  allOption: {
    label: "Select all",
    value: "*"
  },
  allowSelectAll: false,
  onChange: function onChange() {},
  control: undefined,
  value: undefined
};