import type { Env } from '../../env'
import { buildAttachmentContentDisposition } from '../../lib/portal-file-storage'
import {
  createSignedGetUrl as createPlatformSignedGetUrl,
  createSignedPutUrl as createPlatformSignedPutUrl,
  guardMagicBytes as guardPlatformMagicBytes,
  imageDimensions as getPlatformImageDimensions,
} from '@zync/storage'

export function guardMagicBytes(mimeType: string, bytes: Uint8Array): { accepted: boolean } {
  return guardPlatformMagicBytes(mimeType, bytes)
}

export function imageDimensions(bytes: Uint8Array): { width: number; height: number } | null {
  return getPlatformImageDimensions(bytes)
}

export async function presignPut(
  env: Env,
  key: string,
  options: { contentType: string; expiresIn?: number },
): Promise<{ url: string; method: 'PUT'; headers: Record<string, string> }> {
  return createPlatformSignedPutUrl(env, key, options)
}

export async function presignGet(
  env: Env,
  key: string,
  options?: { expiresIn?: number; filename?: string },
): Promise<string> {
  return createPlatformSignedGetUrl(env, key, {
    expiresIn: options?.expiresIn,
    contentDisposition: buildAttachmentContentDisposition(options?.filename ?? 'download'),
  })
}
