import { CurrencyFetchConfig } from "../data/entities.js";
import {
  currencyFetchConfigCreateSchema,
  currencyFetchConfigUpdateSchema
} from "../data/validators.js";
async function createFetchConfig(em, input, scope) {
  const validated = currencyFetchConfigCreateSchema.parse(input);
  const existing = await em.findOne(CurrencyFetchConfig, {
    organizationId: scope.organizationId,
    tenantId: scope.tenantId,
    provider: validated.provider
  });
  if (existing) {
    throw new Error(`Provider ${validated.provider} already configured`);
  }
  const config = em.create(CurrencyFetchConfig, {
    ...validated,
    organizationId: scope.organizationId,
    tenantId: scope.tenantId,
    createdAt: /* @__PURE__ */ new Date(),
    updatedAt: /* @__PURE__ */ new Date()
  });
  await em.persist(config).flush();
  return config;
}
async function updateFetchConfig(em, id, input, scope) {
  const validated = currencyFetchConfigUpdateSchema.parse(input);
  const config = await em.findOne(CurrencyFetchConfig, {
    id,
    organizationId: scope.organizationId,
    tenantId: scope.tenantId
  });
  if (!config) {
    throw new Error("Fetch config not found");
  }
  em.assign(config, validated);
  await em.persist(config).flush();
  return config;
}
async function deleteFetchConfig(em, id, scope) {
  const config = await em.findOne(CurrencyFetchConfig, {
    id,
    organizationId: scope.organizationId,
    tenantId: scope.tenantId
  });
  if (!config) {
    throw new Error("Fetch config not found");
  }
  await em.remove(config).flush();
}
export {
  createFetchConfig,
  deleteFetchConfig,
  updateFetchConfig
};
//# sourceMappingURL=fetch-configs.js.map
