function extractBelongsToTargets(model) {
  const rels = model.relationships ?? {};
  const out = [];
  for (const [name, rawDef] of Object.entries(rels)) {
    const parsed = parseBelongsTo(rawDef);
    if (parsed) out.push({ name, target: parsed });
  }
  return out;
}
function parseBelongsTo(def) {
  if (typeof def === "string") {
    const parts = def.trim().split(/\s+/);
    if (parts[0] === "belongsTo" && parts[1]) return parts[1];
    return null;
  }
  if (def && typeof def === "object") {
    const o = def;
    if (o.type === "belongsTo") {
      return o.target || o.to || o.model || o.targetModel || null;
    }
  }
  return null;
}
function pluralize(s) {
  if (/[^aeiou]y$/i.test(s)) return s.slice(0, -1) + "ies";
  if (/(s|x|z|ch|sh)$/i.test(s)) return s + "es";
  return s + "s";
}
function buildFKMap(model) {
  const map = /* @__PURE__ */ new Map();
  for (const rel of extractBelongsToTargets(model)) {
    map.set(`${rel.name}Id`, rel);
  }
  return map;
}
function extractHasManyTargets(model, modelSchemas) {
  const rels = model.relationships ?? {};
  const out = [];
  const parentLower = model.name.charAt(0).toLowerCase() + model.name.slice(1);
  for (const [name, rawDef] of Object.entries(rels)) {
    const parsed = parseHasMany(rawDef);
    if (!parsed) continue;
    let foreignKey = `${parentLower}Id`;
    if (modelSchemas && modelSchemas[parsed]) {
      const targetRels = modelSchemas[parsed].relationships ?? {};
      for (const [targetRelName, targetRelDef] of Object.entries(targetRels)) {
        const backTarget = parseBelongsTo(targetRelDef);
        if (backTarget === model.name) {
          foreignKey = `${targetRelName}Id`;
          break;
        }
      }
    }
    out.push({ name, target: parsed, foreignKey });
  }
  return out;
}
function parseHasMany(def) {
  if (typeof def === "string") {
    const parts = def.trim().split(/\s+/);
    if (parts[0] === "hasMany" && parts[1]) return parts[1];
    return null;
  }
  if (def && typeof def === "object") {
    const o = def;
    if (o.type === "hasMany") {
      return o.target || o.to || o.model || o.targetModel || null;
    }
  }
  return null;
}
export {
  buildFKMap,
  extractBelongsToTargets,
  extractHasManyTargets,
  pluralize
};
