"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.AutocompleteSelectField = AutocompleteSelectField;
exports.converter = exports.types = void 0;

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _core = require("@material-ui/core");

var _lab = require("@material-ui/lab");

var _rjsfCore = require("@visma/rjsf-core");

var _immer = _interopRequireDefault(require("immer"));

var _react = require("react");

var _styles = require("@material-ui/core/styles");

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

var useStyles = (0, _styles.makeStyles)({
  inputLabelRoot: {
    clip: "rect(0 0 0 0)",
    clipPath: "inset(50%)",
    height: 1,
    overflow: "hidden",
    position: "absolute",
    whiteSpace: "nowrap",
    width: 1
  },
  inputFormControl: {
    "label + &": {
      marginTop: 0
    }
  }
});
var types = ['select', 'selectV2'];
exports.types = types;

function AutocompleteSelectField(props) {
  var _props$uiSchema$uiOp, _props$uiSchema$uiOp$, _props$uiSchema, _props$uiSchema$uiOp3, _props$uiSchema2;

  var classes = useStyles();
  var matchFrom = ((_props$uiSchema$uiOp = props.uiSchema["ui:options"]) === null || _props$uiSchema$uiOp === void 0 ? void 0 : (_props$uiSchema$uiOp$ = _props$uiSchema$uiOp.element) === null || _props$uiSchema$uiOp$ === void 0 ? void 0 : _props$uiSchema$uiOp$.matchFromStart) ? 'start' : 'any';
  var filterOptions = (0, _lab.createFilterOptions)({
    matchFrom: matchFrom,
    ignoreAccents: false
  });

  var _props$uiSchema$uiOp2 = props === null || props === void 0 ? void 0 : (_props$uiSchema = props.uiSchema) === null || _props$uiSchema === void 0 ? void 0 : (_props$uiSchema$uiOp3 = _props$uiSchema['ui:options']) === null || _props$uiSchema$uiOp3 === void 0 ? void 0 : _props$uiSchema$uiOp3.element,
      description = _props$uiSchema$uiOp2.description,
      help = _props$uiSchema$uiOp2.help;

  var rawErrors = props.rawErrors;

  var ariaLabel = _rjsfCore.utils.generateAriaLabel(props.schema.title, (_props$uiSchema2 = props.uiSchema) === null || _props$uiSchema2 === void 0 ? void 0 : _props$uiSchema2['ui:options'], props.required);

  var ariaArray = [ariaLabel];

  if (description) {
    ariaArray.push(description);
  }

  if (help) {
    ariaArray.push(help);
  }

  if (rawErrors) {
    ariaArray.push(rawErrors);
  }

  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_lab.Autocomplete, {
    filterOptions: filterOptions,
    options: props.schema.enumNames,
    value: props.schema.enumNames[props.schema["enum"].indexOf(props.formData)],
    onChange: function onChange(event, value) {
      return props.onChange(props.schema["enum"][props.schema.enumNames.indexOf(value)]);
    },
    getOptionSelected: function getOptionSelected(option, value) {
      return option === value;
    },
    renderInput: function renderInput(params) {
      return /*#__PURE__*/(0, _jsxRuntime.jsx)(_core.TextField, (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, params), {}, {
        hiddenLabel: true,
        label: ariaArray.join(', '),
        InputLabelProps: {
          shrink: false,
          className: classes.inputLabelRoot
        }
      }));
    }
  });
}

var isSameType = function isSameType(a) {
  return function (b) {
    return (a === null || a === void 0 ? void 0 : a.type) === b.type;
  };
};

var isNotSameId = function isNotSameId(a) {
  return function (b) {
    return a.id !== b.id;
  };
};

function UniqueItemsInFormGroupList(props) {
  var _useContext = (0, _react.useContext)(_rjsfCore.utils.Context),
      formData = _useContext.formData;

  var idParts = props.idSchema.$id.split('_');
  var index = Number(idParts[idParts.length - 2]);
  var elements = formData.elements;
  var element = elements[index];
  var illegalIds = elements.filter(isSameType(element)).filter(isNotSameId(element)).map(function (element) {
    return element.id;
  });
  var enumValues = [];
  var enumNames = []; // eslint-disable-next-line @super-template/no-loops/no-loops

  for (var i = 0; i < props.schema["enum"].length; ++i) {
    var enumValue = props.schema["enum"][i];

    if (!illegalIds.includes(enumValue)) {
      enumValues.push(enumValue);
      enumNames.push(props.schema.enumNames[i]);
    }
  }

  return /*#__PURE__*/(0, _jsxRuntime.jsx)(AutocompleteSelectField, (0, _objectSpread2["default"])({}, (0, _immer["default"])(props, function (props) {
    props.schema["enum"] = enumValues;
    props.schema.enumNames = enumNames;
  })));
}

var converter = function converter(_ref) {
  var config = _ref.config;
  return function (props) {
    if (config.autocomplete) {
      var _props$uiSchema3;

      (_props$uiSchema3 = props.uiSchema) !== null && _props$uiSchema3 !== void 0 ? _props$uiSchema3 : props.uiSchema = {};
      props.uiSchema['ui:field'] = config.uniqueItemsInFormGroupList ? UniqueItemsInFormGroupList : AutocompleteSelectField;
    }
  };
};

exports.converter = converter;
//# sourceMappingURL=selectAutocomplete.cjs.map