// NOTE: do not change!!
export enum SyncProviderId {
  'Dropbox' = 'Dropbox',
  'OneDrive' = 'OneDrive',
  'WebDAV' = 'WebDAV',
  'LocalFile' = 'LocalFile',
  'SuperSync' = 'SuperSync',
  'Nextcloud' = 'Nextcloud',
}

/**
 * Providers that authenticate via OAuth and expose `getAuthHelper()` on the
 * runtime provider. Listed here so UI gates (e.g. Re-authenticate button)
 * stay in lockstep with the provider implementations without an async probe.
 */
export const OAUTH_SYNC_PROVIDERS: ReadonlySet<SyncProviderId> = new Set([
  SyncProviderId.Dropbox,
  SyncProviderId.OneDrive,
]);

/**
 * Type-safe conversion from string-based sync provider value to SyncProviderId.
 * Validates that a persisted string value is a valid SyncProviderId at runtime.
 */
export const toSyncProviderId = (
  value: string | null | undefined,
): SyncProviderId | null => {
  if (value === null || value === undefined) return null;
  if (Object.values(SyncProviderId).includes(value as SyncProviderId)) {
    return value as SyncProviderId;
  }
  return null;
};

export enum SyncStatus {
  InSync = 'InSync',
  UpdateRemote = 'UpdateRemote',
  UpdateRemoteAll = 'UpdateRemoteAll',
  UpdateLocal = 'UpdateLocal',
  UpdateLocalAll = 'UpdateLocalAll',
  Conflict = 'Conflict',
  IncompleteRemoteData = 'IncompleteRemoteData',
  NotConfigured = 'NotConfigured',
}

export enum ConflictReason {
  NoLastSync = 'NoLastSync',
  BothNewerLastSync = 'BothNewerLastSync',
  MatchingModelChangeButLastSyncMismatch = 'MatchingModelChangeButLastSyncMismatch',
  UnexpectedRevMismatch = 'UnexpectedRevMismatch',
}

export const REMOTE_FILE_CONTENT_PREFIX = 'pf_' as const;

/**
 * Database key prefix for private configuration storage.
 * Format: PRIVATE_CFG_PREFIX + providerId
 * NOTE: do not change - this is used to store OAuth tokens in IndexedDB!
 */
export const PRIVATE_CFG_PREFIX = '__sp_cred_' as const;
