import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import { TextField } from '@material-ui/core';
import { Autocomplete, createFilterOptions } from '@material-ui/lab';
import { utils } from '@visma/rjsf-core';
import produce from 'immer';
import { useContext } from 'react';
import { makeStyles } from "@material-ui/core/styles";
import { jsx as _jsx } from "react/jsx-runtime";
var useStyles = 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
    }
  }
});
export var types = ['select', 'selectV2'];
export 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 = 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 = 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__*/_jsx(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__*/_jsx(TextField, _objectSpread(_objectSpread({}, 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 = useContext(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__*/_jsx(AutocompleteSelectField, _objectSpread({}, produce(props, function (props) {
    props.schema["enum"] = enumValues;
    props.schema.enumNames = enumNames;
  })));
}

export 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;
    }
  };
};
//# sourceMappingURL=selectAutocomplete.js.map