/**
 * Extract domain from a URL
 */
export function extractDomain(url: string): string {
  try {
    // Add protocol if missing
    if (!url.match(/^https?:\/\//)) {
      url = 'https://' + url;
    }
    
    const urlObj = new URL(url);
    return urlObj.hostname.replace(/^www\./, '');
  } catch {
    // If URL parsing fails, try to extract domain manually
    const match = url.match(/^(?:https?:\/\/)?(?:www\.)?([^\/]+)/);
    return match ? match[1] : url;
  }
}

/**
 * Generate likely domain variations from a company name
 */
export function guessCompanyDomain(name: string): string[] {
  if (!name) return [];
  
  // Remove common company suffixes
  const cleanName = name
    .toLowerCase()
    .replace(/\b(?:pty|ltd|limited|inc|incorporated|corp|corporation|co|company)\b/gi, '')
    .replace(/[^a-z0-9\s]/g, ' ')
    .trim();
  
  // Extract words
  const words = cleanName.split(/\s+/).filter(word => word.length > 1);
  
  const domains: string[] = [];
  
  // Common TLDs for Australian businesses
  const tlds = ['.com.au', '.net.au', '.org.au', '.com', '.net', '.org'];
  
  // Strategy 1: Full company name (without spaces)
  const fullName = words.join('');
  tlds.forEach(tld => {
    domains.push(fullName + tld);
  });
  
  // Strategy 2: First word only (if more than one word)
  if (words.length > 1) {
    tlds.forEach(tld => {
      domains.push(words[0] + tld);
    });
  }
  
  // Strategy 3: First two words (if more than two words)
  if (words.length > 2) {
    const firstTwo = words.slice(0, 2).join('');
    tlds.forEach(tld => {
      domains.push(firstTwo + tld);
    });
  }
  
  // Strategy 4: Common abbreviations
  const abbreviations = words.map(word => word[0]).join('');
  if (abbreviations.length >= 2 && abbreviations.length <= 5) {
    tlds.forEach(tld => {
      domains.push(abbreviations + tld);
    });
  }
  
  // Strategy 5: Building/Construction specific variations
  const industryWords = ['building', 'construction', 'construct', 'builders', 'group'];
  const hasIndustryWord = words.some(word => industryWords.includes(word));
  
  if (!hasIndustryWord) {
    // Add 'builders' to the end
    tlds.forEach(tld => {
      domains.push(fullName + 'builders' + tld);
      if (words.length > 1) {
        domains.push(words[0] + 'builders' + tld);
      }
    });
  }
  
  // Remove duplicates and return
  return [...new Set(domains)];
}

/**
 * Try to find company website from ABR data
 * Note: This is a placeholder - ABR API doesn't typically provide website URLs
 * You may need to integrate with other services or databases
 */
export function extractWebsiteFromABR(abrData: any): string | null {
  // ABR typically doesn't provide website information
  // This is a placeholder for future integration with other data sources
  return null;
}