import {
  registerModuleOverrideApplier
} from "@open-mercato/shared/modules/overrides";
const programmaticAgentOverrides = {};
const programmaticToolOverrides = {};
const programmaticAgentExtensions = [];
const modulesConfigAgentOverrides = {};
const modulesConfigToolOverrides = {};
const modulesConfigAgentExtensions = [];
function applyAiAgentOverrides(overrides) {
  for (const [id, value] of Object.entries(overrides)) {
    programmaticAgentOverrides[id] = value;
  }
}
function applyAiToolOverrides(overrides) {
  for (const [name, value] of Object.entries(overrides)) {
    programmaticToolOverrides[name] = value;
  }
}
function applyAiAgentExtensions(extensions) {
  programmaticAgentExtensions.push(...extensions);
}
function applyAiOverridesFromEnabledModules(modules) {
  for (const entry of modules) {
    if (!entry || typeof entry.id !== "string" || !entry.id) continue;
    const ai = entry.overrides?.ai;
    if (!ai || typeof ai !== "object") continue;
    if (ai.agents && typeof ai.agents === "object") {
      for (const [id, value] of Object.entries(ai.agents)) {
        modulesConfigAgentOverrides[id] = value;
      }
    }
    if (ai.tools && typeof ai.tools === "object") {
      for (const [name, value] of Object.entries(ai.tools)) {
        modulesConfigToolOverrides[name] = value;
      }
    }
    if (Array.isArray(ai.extensions)) {
      modulesConfigAgentExtensions.push(...ai.extensions);
    }
  }
}
function applyAiOverridesDispatcherEntries(entries) {
  applyAiOverridesFromEnabledModules(
    entries.map((entry) => ({
      id: entry.moduleId,
      overrides: { ai: entry.overrides }
    }))
  );
}
registerModuleOverrideApplier(
  "ai",
  (entries) => {
    applyAiOverridesDispatcherEntries(entries);
  }
);
function resetProgrammaticOverridesForTests() {
  for (const key of Object.keys(programmaticAgentOverrides)) {
    delete programmaticAgentOverrides[key];
  }
  for (const key of Object.keys(programmaticToolOverrides)) {
    delete programmaticToolOverrides[key];
  }
  for (const key of Object.keys(modulesConfigAgentOverrides)) {
    delete modulesConfigAgentOverrides[key];
  }
  for (const key of Object.keys(modulesConfigToolOverrides)) {
    delete modulesConfigToolOverrides[key];
  }
  programmaticAgentExtensions.length = 0;
  modulesConfigAgentExtensions.length = 0;
}
function composeAgentOverrideMap(fileEntries) {
  const out = {};
  for (const entry of fileEntries) {
    const overrides = entry?.overrides;
    if (!overrides || typeof overrides !== "object") continue;
    for (const [id, value] of Object.entries(overrides)) {
      if (typeof id !== "string" || !id) continue;
      out[id] = value;
    }
  }
  for (const [id, value] of Object.entries(modulesConfigAgentOverrides)) {
    out[id] = value;
  }
  for (const [id, value] of Object.entries(programmaticAgentOverrides)) {
    out[id] = value;
  }
  return out;
}
function composeToolOverrideMap(fileEntries) {
  const out = {};
  for (const entry of fileEntries) {
    const overrides = entry?.overrides;
    if (!overrides || typeof overrides !== "object") continue;
    for (const [name, value] of Object.entries(overrides)) {
      if (typeof name !== "string" || !name) continue;
      out[name] = value;
    }
  }
  for (const [name, value] of Object.entries(modulesConfigToolOverrides)) {
    out[name] = value;
  }
  for (const [name, value] of Object.entries(programmaticToolOverrides)) {
    out[name] = value;
  }
  return out;
}
function composeAgentExtensionEntries(fileEntries) {
  const out = [];
  for (const entry of fileEntries) {
    if (Array.isArray(entry?.extensions)) out.push(...entry.extensions);
  }
  out.push(...modulesConfigAgentExtensions);
  out.push(...programmaticAgentExtensions);
  return out;
}
function applyAgentOverrideMap(base, overrides) {
  if (!overrides || Object.keys(overrides).length === 0) return base.slice();
  const byId = /* @__PURE__ */ new Map();
  for (const agent of base) {
    if (agent && typeof agent.id === "string" && agent.id) {
      byId.set(agent.id, agent);
    }
  }
  for (const [id, value] of Object.entries(overrides)) {
    if (!byId.has(id) && value !== null) {
      console.warn(
        `[AI Overrides] Override registers a new agent "${id}" \u2014 no base entry to replace.`
      );
    }
    if (value === null) {
      byId.delete(id);
      continue;
    }
    if (!value || typeof value.id !== "string" || value.id !== id) {
      console.warn(
        `[AI Overrides] Skipping malformed agent override for id "${id}" \u2014 id mismatch or missing fields.`
      );
      continue;
    }
    byId.set(id, value);
  }
  return Array.from(byId.values());
}
function applyToolOverrideMap(base, overrides) {
  const out = new Map(base);
  if (!overrides) return out;
  for (const [name, value] of Object.entries(overrides)) {
    if (value === null) {
      out.delete(name);
      continue;
    }
    if (value === void 0) continue;
    if (!value || typeof value.name !== "string" || value.name !== name) {
      console.warn(
        `[AI Overrides] Skipping malformed tool override for name "${name}" \u2014 name mismatch or missing fields.`
      );
      continue;
    }
    out.set(name, value);
  }
  return out;
}
function snapshotProgrammaticOverrides() {
  return {
    agents: { ...programmaticAgentOverrides },
    tools: { ...programmaticToolOverrides },
    modulesConfigAgents: { ...modulesConfigAgentOverrides },
    modulesConfigTools: { ...modulesConfigToolOverrides },
    agentExtensions: programmaticAgentExtensions.slice(),
    modulesConfigAgentExtensions: modulesConfigAgentExtensions.slice()
  };
}
export {
  applyAgentOverrideMap,
  applyAiAgentExtensions,
  applyAiAgentOverrides,
  applyAiOverridesDispatcherEntries,
  applyAiOverridesFromEnabledModules,
  applyAiToolOverrides,
  applyToolOverrideMap,
  composeAgentExtensionEntries,
  composeAgentOverrideMap,
  composeToolOverrideMap,
  resetProgrammaticOverridesForTests,
  snapshotProgrammaticOverrides
};
//# sourceMappingURL=ai-overrides.js.map
