// Pins the pure resolution logic behind "save media to Photos":
//  - images prefer remote_url (federated), video uses url (never remote_url)
//  - HLS manifests are unsupported
//  - file extensions are inferred correctly (saveToLibraryAsync keys on them)

import type { MediaAttachment } from 'src/lib/api-types'
import { inferExtension, resolveSaveTarget } from 'src/lib/media/saveTarget'

function att(partial: Partial<MediaAttachment>): MediaAttachment {
  return {
    id: '1',
    type: 'image',
    url: 'https://cdn.example/local.jpg',
    preview_url: null,
    remote_url: null,
    meta: {} as MediaAttachment['meta'],
    description: null,
    blurhash: null,
    text_url: undefined,
    ...partial,
  } as MediaAttachment
}

describe('resolveSaveTarget', () => {
  it('image prefers remote_url for federated media', () => {
    expect(
      resolveSaveTarget({
        attachment: att({
          type: 'image',
          url: 'https://local/cached.jpg',
          remote_url: 'https://cdn/orig.jpg',
        }),
      })
    ).toEqual({ kind: 'image', url: 'https://cdn/orig.jpg' })
  })

  it('image falls back to url when no remote_url', () => {
    expect(
      resolveSaveTarget({ attachment: att({ type: 'image', url: 'https://cdn/a.jpg' }) })
    ).toEqual({ kind: 'image', url: 'https://cdn/a.jpg' })
  })

  it('video uses url, never remote_url', () => {
    expect(
      resolveSaveTarget({
        attachment: att({
          type: 'video',
          url: 'https://cdn/video.mp4',
          remote_url: 'https://up/weird.mkv',
        }),
      })
    ).toEqual({ kind: 'video', url: 'https://cdn/video.mp4' })
  })

  it('treats gifv as video', () => {
    expect(
      resolveSaveTarget({ attachment: att({ type: 'gifv', url: 'https://cdn/loop.mp4' }) })
    ).toEqual({ kind: 'video', url: 'https://cdn/loop.mp4' })
  })

  it('marks HLS manifests unsupported', () => {
    expect(
      resolveSaveTarget({ attachment: att({ type: 'video', url: 'https://cdn/s.m3u8' }) })
    ).toEqual({ unsupported: true })
  })

  it('passes a direct uri+kind through (lightbox path)', () => {
    expect(resolveSaveTarget({ uri: 'https://cdn/pic.png', kind: 'image' })).toEqual({
      kind: 'image',
      url: 'https://cdn/pic.png',
    })
  })

  it('marks a direct m3u8 uri unsupported', () => {
    expect(resolveSaveTarget({ uri: 'https://cdn/s.m3u8', kind: 'video' })).toEqual({
      unsupported: true,
    })
  })

  it('marks empty input unsupported', () => {
    expect(resolveSaveTarget({})).toEqual({ unsupported: true })
  })
})

describe('inferExtension', () => {
  it('keeps a known image extension', () => {
    expect(inferExtension('https://cdn/a.png', 'image')).toBe('png')
  })

  it('strips the query string before reading the extension', () => {
    expect(inferExtension('https://cdn/a.jpg?v=2&x=1', 'image')).toBe('jpg')
  })

  it('keeps a known video extension', () => {
    expect(inferExtension('https://cdn/a.mov', 'video')).toBe('mov')
  })

  it('defaults an extensionless image url to jpg', () => {
    expect(inferExtension('https://cdn/file', 'image')).toBe('jpg')
  })

  it('defaults an extensionless video url to mp4', () => {
    expect(inferExtension('https://cdn/file', 'video')).toBe('mp4')
  })

  it('ignores a non-media extension and uses the kind default', () => {
    expect(inferExtension('https://cdn/a.bin', 'image')).toBe('jpg')
  })
})
