import { findOneWithDecryption } from "@open-mercato/shared/lib/encryption/find";
import { CatalogProduct } from "../data/entities.js";
const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
function normalizeLookupValue(value) {
  return typeof value === "string" ? value.trim() : "";
}
async function resolveProductBySkuOrExternalId(em, lookup, scope) {
  const externalId = normalizeLookupValue(lookup.externalId);
  const sku = normalizeLookupValue(lookup.sku);
  if (!externalId && !sku) return null;
  if (externalId && UUID_REGEX.test(externalId)) {
    const byExternalId = await findOneWithDecryption(
      em,
      CatalogProduct,
      {
        id: externalId,
        organizationId: scope.organizationId,
        tenantId: scope.tenantId,
        deletedAt: null
      },
      void 0,
      scope
    );
    if (byExternalId) return byExternalId;
  }
  if (sku) {
    return findOneWithDecryption(
      em,
      CatalogProduct,
      {
        sku,
        organizationId: scope.organizationId,
        tenantId: scope.tenantId,
        deletedAt: null
      },
      void 0,
      scope
    );
  }
  return null;
}
export {
  resolveProductBySkuOrExternalId
};
//# sourceMappingURL=productResolution.js.map
