export type IntentType = 'SEARCH' | 'VIDEO_PARSE' | 'CLOUD_SAVE';

export interface IntentResult {
  type: IntentType;
  platform?: string;
  rawInput: string;
  cleanInput: string;
}

const VIDEO_PLATFORMS = [
  { domain: /douyin\.com|v\.douyin\.com/i, name: 'douyin' },
  { domain: /kuaishou\.com/i, name: 'kuaishou' },
  { domain: /xiaohongshu\.com/i, name: 'xiaohongshu' },
  { domain: /bilibili\.com|b23\.tv/i, name: 'bilibili' },
  { domain: /weibo\.com/i, name: 'weibo' },
  { domain: /pipixia\.com/i, name: 'pipixia' },
  { domain: /y\.qq\.com/i, name: 'qqmusic' },
];

import { CLOUD_DOMAIN_PATTERNS } from '../config/cloud-labels';

export function detectIntent(input: string): IntentResult {
  const urlMatch = input.match(/(https?:\/\/[^\s]+)/i);
  if (urlMatch) {
    const url = urlMatch[1];

    for (const p of VIDEO_PLATFORMS) {
      if (p.domain.test(url)) {
        return { type: 'VIDEO_PARSE', platform: p.name, rawInput: input, cleanInput: url };
      }
    }

    for (const p of CLOUD_DOMAIN_PATTERNS) {
      if (p.regex.test(url)) {
        return { type: 'CLOUD_SAVE', platform: p.type, rawInput: input, cleanInput: url };
      }
    }
  }

  return { type: 'SEARCH', rawInput: input, cleanInput: input.trim() };
}
