import { getPropertyByString as l } from "@m4l/core";
import { isPositiveInteger as u } from "../../../isPositiveInteger.js";
import { isRecord as m } from "../../../isRecord.js";
import { resolveRemoteOrAbsoluteAssetSrc as s } from "../../helpers/resolveRemoteOrAbsoluteAssetSrc.js";
import { resolveFieldInputLabel as f } from "../../../resolveFieldInputLabel/resolveFieldInputLabel.js";
import { createIsOptionEqualToValue as p } from "../../../createIsOptionEqualToValue/createIsOptionEqualToValue.js";
function g(r) {
  return Array.isArray(r) ? r : [];
}
function c(r, o) {
  if (typeof r == "string" || typeof r == "number")
    return String(r);
  if (!m(r))
    return "";
  for (const n of o) {
    const t = l(r, n);
    if (typeof t == "string" && t.trim() !== "")
      return t;
    if (typeof t == "number")
      return String(t);
  }
  return "";
}
function d(r) {
  if (r && r.trim() !== "") {
    const o = r.trim();
    return (n) => {
      if (!m(n))
        return "";
      const t = l(n, o);
      return typeof t == "string" && t.trim() !== "" ? t : typeof t == "number" ? String(t) : "";
    };
  }
  return (o) => c(o, ["label", "name", "value", "id"]);
}
function y(r, o) {
  function n(t) {
    return typeof t != "string" || t.trim() === "" ? "" : s(o, t);
  }
  if (r && r.trim() !== "") {
    const t = r.trim();
    return (a) => m(a) ? n(l(a, t)) : "";
  }
  return (t) => {
    if (!m(t))
      return "";
    for (const a of ["imageUrl", "image", "src", "url", "path"]) {
      const e = n(l(t, a));
      if (e !== "")
        return e;
    }
    return "";
  };
}
function T(r, o, n) {
  const t = f(r, o), a = g(r.options), e = r.fieldType.componentProps, i = {};
  return m(e) && (typeof e.id == "string" && e.id.trim() !== "" && (i.id = e.id.trim()), (e.presentationType === "standard" || e.presentationType === "grid") && (i.presentationType = e.presentationType), u(e.imageSize) && (i.imageSize = e.imageSize), u(e.maxColumns) && (i.maxColumns = e.maxColumns), u(e.maxRows) && (i.maxRows = e.maxRows), typeof e.refetchOnOpen == "boolean" && (i.refetchOnOpen = e.refetchOnOpen), u(e.timeout) && (i.timeout = e.timeout), typeof e.resultField == "string" && e.resultField.trim() !== "" && (i.resultField = e.resultField.trim()), typeof e.optionLabelPath == "string" && e.optionLabelPath.trim() !== "" && (i.optionLabelPath = e.optionLabelPath.trim()), typeof e.optionImagePath == "string" && e.optionImagePath.trim() !== "" && (i.optionImagePath = e.optionImagePath.trim()), typeof e.equalToValuePath == "string" && e.equalToValuePath.trim() !== "" && (i.equalToValuePath = e.equalToValuePath.trim())), {
    ...i,
    resolvedLabel: t,
    resolvedOptions: a,
    resolvedGetOptionLabel: d(i.optionLabelPath),
    resolvedGetOptionImage: y(i.optionImagePath, n),
    resolvedIsOptionEqualToValue: p(
      i.equalToValuePath ?? "id"
    )
  };
}
export {
  T as normalizeImageSelectorPropsFromBackend
};
