import {
  __decorateClass
} from "./chunk-2E224ZSN.js";

// runtime/subsystems/integration/deep-equal.differ.ts
import { Injectable } from "@nestjs/common";
var DEFAULT_IGNORE_FIELDS = /* @__PURE__ */ new Set([
  "id",
  "createdAt",
  "updatedAt",
  "deletedAt",
  "type",
  "lastModifiedAt",
  "fields",
  "external_id",
  "externalId",
  "provider",
  "provider_metadata",
  "providerMetadata"
]);
var DeepEqualDiffer = class {
  ignore;
  constructor(opts = {}) {
    if (opts.ignore && opts.ignore.length > 0) {
      this.ignore = /* @__PURE__ */ new Set([...DEFAULT_IGNORE_FIELDS, ...opts.ignore]);
    } else {
      this.ignore = DEFAULT_IGNORE_FIELDS;
    }
  }
  diff(existing, incoming, providerChangedFields) {
    if (existing === null) {
      const out2 = {};
      for (const key of Object.keys(incoming)) {
        if (this.ignore.has(key)) continue;
        const value = incoming[key];
        if (value === null || value === void 0) continue;
        out2[key] = { from: null, to: value };
      }
      return Object.keys(out2).length === 0 ? "noop" : out2;
    }
    const candidates = /* @__PURE__ */ new Set();
    if (providerChangedFields && providerChangedFields.length > 0) {
      for (const key of providerChangedFields) {
        if (!this.ignore.has(key)) candidates.add(key);
      }
    } else {
      for (const key of Object.keys(incoming)) {
        if (!this.ignore.has(key)) candidates.add(key);
      }
      for (const key of Object.keys(existing)) {
        if (this.ignore.has(key)) continue;
        if (!(key in incoming)) continue;
        candidates.add(key);
      }
    }
    const out = {};
    for (const key of candidates) {
      const before = existing[key];
      const after = incoming[key];
      if (!isEqual(before, after)) {
        out[key] = { from: before ?? null, to: after ?? null };
      }
    }
    return Object.keys(out).length === 0 ? "noop" : out;
  }
};
DeepEqualDiffer = __decorateClass([
  Injectable()
], DeepEqualDiffer);
function isEqual(a, b) {
  if (a === b) return true;
  const na = normalize(a);
  const nb = normalize(b);
  if (na === nb) return true;
  if (typeof na === "object" && typeof nb === "object" && na !== null && nb !== null) {
    return deepEqualObject(na, nb);
  }
  const numericEqual = maybeNumericEqual(na, nb) || maybeNumericEqual(nb, na);
  return numericEqual;
}
function normalize(value) {
  if (value instanceof Date) return value.toISOString();
  return value;
}
function maybeNumericEqual(a, b) {
  if (typeof a !== "string" || typeof b !== "number") return false;
  if (a.trim() === "") return false;
  const parsed = Number(a);
  if (!Number.isFinite(parsed)) return false;
  return parsed === b;
}
function deepEqualObject(a, b) {
  if (Array.isArray(a) !== Array.isArray(b)) return false;
  const aKeys = Object.keys(a);
  const bKeys = Object.keys(b);
  if (aKeys.length !== bKeys.length) return false;
  for (const key of aKeys) {
    if (!(key in b)) return false;
    if (!isEqual(a[key], b[key])) return false;
  }
  return true;
}

export {
  DeepEqualDiffer
};
//# sourceMappingURL=chunk-36U5UGIO.js.map