{"version":3,"file":"conflict.cjs","names":[],"sources":["../../src/offline/conflict.ts"],"sourcesContent":["/**\n * Conflict-resolution helpers for the `applyRemote` callback of\n * {@link createOfflineSync}. Each takes the current local record (or\n * `undefined` when the record is new locally) and the incoming remote record,\n * and returns the winner. Ties resolve to the remote copy, treating the server\n * as authoritative.\n */\n\n/** Normalise a timestamp-ish value to epoch milliseconds. */\nfunction toMillis(value: number | string | Date): number {\n    if (value instanceof Date) return value.getTime();\n    if (typeof value === \"number\") return value;\n    const parsed = Date.parse(value);\n    return Number.isNaN(parsed) ? 0 : parsed;\n}\n\n/**\n * Resolve a conflict by keeping whichever record was written last, comparing a\n * timestamp field. The remote record wins ties.\n *\n * @typeParam T - The record shape.\n * @param local - The current local record, or `undefined` when new.\n * @param remote - The incoming remote record.\n * @param getTimestamp - Reads the comparison timestamp (epoch ms, ISO string or `Date`).\n * @returns The winning record.\n *\n * @example\n * applyRemote: async (dto) => {\n *     const local = await store.get(dto.id);\n *     await store.save(lastWriteWins(local, dto, (r) => r.updatedAt));\n * }\n */\nexport function lastWriteWins<T>(\n    local: T | undefined,\n    remote: T,\n    getTimestamp: (record: T) => number | string | Date,\n): T {\n    if (local === undefined) return remote;\n    return toMillis(getTimestamp(remote)) >= toMillis(getTimestamp(local)) ? remote : local;\n}\n\n/**\n * Resolve a conflict by keeping whichever record carries the higher version\n * number (monotonic counter / row version). The remote record wins ties.\n *\n * @typeParam T - The record shape.\n * @param local - The current local record, or `undefined` when new.\n * @param remote - The incoming remote record.\n * @param getVersion - Reads the monotonic version number.\n * @returns The winning record.\n *\n * @example\n * applyRemote: async (dto) => {\n *     const local = await store.get(dto.id);\n *     await store.save(higherVersionWins(local, dto, (r) => r.version));\n * }\n */\nexport function higherVersionWins<T>(\n    local: T | undefined,\n    remote: T,\n    getVersion: (record: T) => number,\n): T {\n    if (local === undefined) return remote;\n    return getVersion(remote) >= getVersion(local) ? remote : local;\n}\n"],"mappings":"AASA,SAAS,EAAS,EAAuC,CACrD,GAAI,aAAiB,KAAM,OAAO,EAAM,QAAQ,EAChD,GAAI,OAAO,GAAU,SAAU,OAAO,EACtC,IAAM,EAAS,KAAK,MAAM,CAAK,EAC/B,OAAO,OAAO,MAAM,CAAM,EAAI,EAAI,CACtC,CAkBA,SAAgB,EACZ,EACA,EACA,EACC,CAED,OADI,IAAU,IAAA,IACP,EAAS,EAAa,CAAM,CAAC,GAAK,EAAS,EAAa,CAAK,CAAC,EADrC,EACkD,CACtF,CAkBA,SAAgB,EACZ,EACA,EACA,EACC,CAED,OADI,IAAU,IAAA,IACP,EAAW,CAAM,GAAK,EAAW,CAAK,EADb,EAC0B,CAC9D"}