import { ref } from 'vue'

export interface MarketplaceAppInfo {
  id: string
  title: string
  version: string
  icon: string
  category: string
  description: string | { short: string; long: string }
  author: string
  source: string
  manifestUrl: string
  url: string
  repoUrl: string
  s9pkUrl: string
}

// Simple in-memory store for the current marketplace app
const currentMarketplaceApp = ref<MarketplaceAppInfo | null>(null)

export function useMarketplaceApp() {
  function setCurrentApp(app: Partial<MarketplaceAppInfo> & { id: string }) {
    // Create a clean, serializable copy
    currentMarketplaceApp.value = {
      id: app.id,
      title: app.title ?? '',
      version: app.version ?? '',
      icon: app.icon ?? '',
      category: app.category ?? '',
      description: app.description ?? '',
      author: app.author ?? '',
      source: app.source ?? '',
      manifestUrl: app.manifestUrl || app.s9pkUrl || app.url || '',
      url: app.url || app.s9pkUrl || app.manifestUrl || '',
      repoUrl: app.repoUrl ?? '',
      s9pkUrl: app.s9pkUrl ?? '',
    }
  }

  function getCurrentApp() {
    return currentMarketplaceApp.value
  }

  function clearCurrentApp() {
    currentMarketplaceApp.value = null
  }

  return {
    setCurrentApp,
    getCurrentApp,
    clearCurrentApp
  }
}
