import https from 'https';

const keywords = [
  // German Core
  'receipt scanner',
  'beleg scanner',
  'belege digitalisieren',
  'rechnung scanner',
  'kassenbon scanner',
  'quittung scanner',
  'spesen app',
  'buchhaltung scanner',
  'datev',
  'datev scanner',
  'belegmanager',
  'haushaltsbuch',
  'ausgaben tracker',
  'steuer belege',
  
  // English Core
  'receipt to excel',
  'receipt scanner to excel',
  'expense tracker',
  'receipt keeper',
  'invoice scanner',
  'smart receipts',
  'receipts',
  'shoeboxed',
  'expensify',
  'camscanner',
  'genius scan'
];

function fetchHints(term, storefront = '143443') { // 143443 = Germany, 143441 = US
  return new Promise((resolve) => {
    const url = `https://search.itunes.apple.com/WebObjects/MZSearchHints.woa/wa/hints?clientApplication=Software&term=${encodeURIComponent(term)}`;
    https.get(url, {
      headers: {
        'User-Agent': 'AppStore/3.0 (iOS; 16.0)',
        'X-Apple-Store-Front': `${storefront}-1,29`
      }
    }, (res) => {
      let data = '';
      res.on('data', chunk => data += chunk);
      res.on('end', () => {
        try {
          const json = JSON.parse(data);
          resolve(json);
        } catch (e) {
          resolve(null);
        }
      });
    }).on('error', () => resolve(null));
  });
}

async function test() {
  console.log('Testing hints:');
  for (const kw of ['beleg', 'kassenbon', 'receipt', 'expense', 'datev', 'spesen', 'rechnung']) {
    const hintsDE = await fetchHints(kw, '143443');
    console.log(`DE Hints for "${kw}":`, hintsDE?.hints?.map(h => h.term));
    const hintsUS = await fetchHints(kw, '143441');
    console.log(`US Hints for "${kw}":`, hintsUS?.hints?.map(h => h.term));
  }
}

test();
