import { Component, useEntity, useEnvironment, QueryLanguage, Field } from "@contember/binding";
import { useMemo } from "react";
import { FieldFallbackView } from "./FieldFallbackView.js";
import { jsx, Fragment } from "react/jsx-runtime";
const CoalesceFieldView = Component((props) => {
  const entity = useEntity();
  const environment = useEnvironment();
  const desugaredFields = useMemo(
    () => props.fields.map((it) => QueryLanguage.desugarRelativeSingleField(it, environment)),
    [environment, ...props.fields]
  );
  let value = null;
  for (const field of desugaredFields) {
    value = entity.getRelativeSingleField(field).value;
    if (value !== null) {
      break;
    }
  }
  if (value === null) {
    return /* @__PURE__ */ jsx(FieldFallbackView, {
      fallback: props.fallback,
      fallbackStyle: props.fallbackStyle
    });
  }
  if (props.format) {
    return /* @__PURE__ */ jsx(Fragment, {
      children: props.format(value)
    });
  }
  return /* @__PURE__ */ jsx(Fragment, {
    children: value
  });
}, (props) => {
  return /* @__PURE__ */ jsx(Fragment, {
    children: props.fields.map((it, i) => /* @__PURE__ */ jsx(Field, {
      field: it
    }, i))
  });
});
export {
  CoalesceFieldView
};
//# sourceMappingURL=CoalesceFieldView.js.map
