import { getPropertyByString as A } from "@m4l/core";
import { createDateRangeValue as s } from "../../../DateRangeCalendarPicker/helpers.js";
import { EMPTY_DATE_TIME_RANGE_PICKER_VALUE as f } from "../../../DateTimeRangePicker/constants.js";
import { isDynamicFormDateRangeField as D } from "../isDateRangeField.js";
import { isRecord as l } from "../isRecord.js";
import { resolveFieldTypeComponentProps as y } from "../resolveFieldContractAliases.js";
function p(r, i) {
  if (typeof r == "number" || typeof r == "boolean")
    return r;
  if (i === "fft_boolean" || i === "fft_checkbox") {
    const n = r.trim().toLowerCase();
    return n === "true" ? !0 : n === "false" ? !1 : !!r;
  }
  const t = Number(r);
  return !isNaN(t) && r.trim() !== "" ? t : r;
}
function o(r) {
  if (r == null || r === "")
    return null;
  if (r instanceof Date)
    return Number.isNaN(r.getTime()) ? null : new Date(r);
  if (typeof r == "number" || typeof r == "string") {
    const i = new Date(r);
    return Number.isNaN(i.getTime()) ? null : i;
  }
  return null;
}
function c(r) {
  if (Array.isArray(r))
    return r.length !== 2 ? null : s(
      o(r[0]),
      o(r[1])
    );
  if (typeof r == "string")
    try {
      return c(JSON.parse(r));
    } catch {
      return null;
    }
  return l(r) ? "startDate" in r || "endDate" in r ? s(
    o(r.startDate),
    o(r.endDate)
  ) : "start" in r || "end" in r ? s(
    o(r.start),
    o(r.end)
  ) : null : null;
}
function m(r, i) {
  if (!Array.isArray(r.options))
    return null;
  const t = y(r.fieldType), n = typeof t?.equalToValuePath == "string" && t.equalToValuePath.trim() !== "" ? t.equalToValuePath.trim() : "id", u = r.options.find((e) => l(e) ? String(A(e, n)) === String(i) : !1);
  return l(u) ? u : null;
}
function S(r) {
  if (!r.fieldType)
    return null;
  const i = y(r.fieldType), t = r.fieldType?.componentId, n = r.fieldType?.id, u = D(r);
  if (r.currentValue !== null && r.isVisible !== !1) {
    const e = r.currentValue, V = t === "RHFCheckableList" || t === "RHFDaysOfWeekPicker", g = t === "RHFAutoComplete", R = t === "RHFImageSelector", b = t === "RHFSelect" && i?.isMultiple === !0;
    if (g)
      return;
    if (u)
      return c(e) ?? f;
    if (R && (typeof e == "string" || typeof e == "number"))
      return m(r, e);
    if (V || b) {
      if (Array.isArray(e))
        return e;
      if (typeof e == "string")
        try {
          const a = JSON.parse(e);
          if (Array.isArray(a))
            return a;
        } catch {
          const a = Number(e);
          return !isNaN(a) && e.trim() !== "" ? [a] : [e];
        }
      return [e];
    }
    return Array.isArray(e) ? e : p(e, n);
  }
  if (r.defaultValue !== null && r.defaultValue !== void 0) {
    if (u)
      return c(r.defaultValue) ?? f;
    if (t === "RHFImageSelector" && (typeof r.defaultValue == "string" || typeof r.defaultValue == "number"))
      return m(r, r.defaultValue);
    if (t === "RHFCheckableList" || t === "RHFDaysOfWeekPicker") {
      if (typeof r.defaultValue == "string")
        try {
          const e = JSON.parse(r.defaultValue);
          if (Array.isArray(e))
            return e;
        } catch {
          const e = Number(r.defaultValue);
          return !isNaN(e) && r.defaultValue.trim() !== "" ? [e] : [r.defaultValue];
        }
      return [r.defaultValue];
    }
    return p(r.defaultValue, n);
  }
  return t === "RHFCheckableList" || t === "RHFDaysOfWeekPicker" ? [] : t === "RHFAutoComplete" ? i?.isMultiple === !0 ? [] : null : t === "RHFImageSelector" || t === "RHFRadioGroup" ? null : u ? f : n === "fft_boolean" || n === "fft_checkbox" ? !1 : null;
}
export {
  S as resolveInitialValue
};
