import type { JobData } from '../types/api'

export type InlineJobFilter = {
  templateType: string
  scopeId: string
}

/** Jobs already shown inline on the current page — hide from 任務觀察站 to avoid duplicate UI. */
export function getInlineJobsForPath(pathname: string, search: string): InlineJobFilter[] {
  const params = new URLSearchParams(search)
  const brandFromQuery = params.get('brand')?.trim() || ''

  const topicResearch = pathname.match(/^\/placement\/topics\/([^/]+)\/research-map$/)
  if (topicResearch) {
    const scopeId = topicResearch[1]
    return [
      { templateType: 'expand-graph', scopeId },
      { templateType: 'placement-scan', scopeId },
    ]
  }

  if (pathname === '/research' && brandFromQuery) {
    return [
      { templateType: 'expand-graph', scopeId: brandFromQuery },
      { templateType: 'placement-scan', scopeId: brandFromQuery },
    ]
  }

  if (pathname === '/outreach' && brandFromQuery) {
    return [{ templateType: 'placement-scan', scopeId: brandFromQuery }]
  }

  const personaMatch = pathname.match(/^\/personas\/([^/]+)$/)
  if (personaMatch) {
    return [{ templateType: 'style-8d', scopeId: personaMatch[1] }]
  }

  return []
}

export function jobShownInline(job: JobData, filters: InlineJobFilter[]): boolean {
  if (filters.length === 0) return false
  return filters.some(
    (filter) => filter.templateType === job.template_type && filter.scopeId === job.scope_id,
  )
}