import { useMemo, createElement } from "react";
import { Entity } from "@contember/binding";
import { useSearchFields } from "./useSearchFields.js";
const useNormalizedOptions = (optionEntities, desugaredOptionPath, { searchByFields, ...props }) => {
  const desugaredSearchFields = useSearchFields(searchByFields);
  const renderOption = "renderOption" in props && props.renderOption ? props.renderOption : void 0;
  const optionLabel = "optionLabel" in props && props.optionLabel ? props.optionLabel : void 0;
  return useMemo(() => optionEntities.map((item, i) => {
    let label;
    if (renderOption) {
      label = renderOption(item);
    } else if (optionLabel) {
      label = /* @__PURE__ */ createElement(Entity, {
        accessor: item
      }, " ", optionLabel, " ");
    } else if ("field" in desugaredOptionPath) {
      label = `${item.getRelativeSingleField(desugaredOptionPath).value ?? ""}`;
    } else {
      label = "";
    }
    let searchKeywords;
    if (desugaredSearchFields.length) {
      searchKeywords = desugaredSearchFields.map((desugared) => item.getRelativeSingleField(desugared).value ?? "").join(" ");
    } else if (typeof label === "string") {
      searchKeywords = label;
    } else {
      searchKeywords = "";
    }
    return {
      key: item.id.toString(),
      label,
      searchKeywords,
      value: item
    };
  }), [optionEntities, renderOption, optionLabel, desugaredOptionPath, desugaredSearchFields]);
};
export { useNormalizedOptions };
//# sourceMappingURL=useNormalizedOptions.js.map
