/**
 * Backup destination transport factory. Given a destination type + its
 * *decrypted* runtime config, build the matching transport.
 */

import { FilesystemTransport } from './filesystem';
import { S3Transport } from './s3';
import { SftpTransport } from './sftp';
import type {
  BackupDestinationType,
  BackupTransport,
  FilesystemDestConfig,
  S3DestConfig,
  SftpDestConfig,
} from './types';

export function buildTransport(
  type: BackupDestinationType,
  config: Record<string, unknown>,
): BackupTransport {
  switch (type) {
    case 'filesystem':
      return new FilesystemTransport(config as unknown as FilesystemDestConfig);
    case 'sftp':
      return new SftpTransport(config as unknown as SftpDestConfig);
    case 's3':
      return new S3Transport(config as unknown as S3DestConfig);
    default:
      throw new Error(`Unknown backup destination type: ${String(type)}`);
  }
}

export { FilesystemTransport } from './filesystem';
export { SftpTransport } from './sftp';
export { S3Transport, parseS3Endpoint } from './s3';
export * from './types';
