import { useMemo } from "react";
import { Entity } from "@contember/binding";
import { useSearchFields } from "./useSearchFields.js";
import { jsxDEV } from "react/jsx-dev-runtime";
var _jsxFileName = "/__w/interface/interface/packages/admin/src/components/bindingFacade/fields/ChoiceField/hooks/useNormalizedOptions.tsx";
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__ */ jsxDEV(Entity, {
        accessor: item,
        children: [" ", optionLabel, " "]
      }, void 0, true, {
        fileName: _jsxFileName,
        lineNumber: 23,
        columnNumber: 14
      }, void 0);
    } 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
