import fs from 'fs';
import path from 'path';

function transform(data: any) {
  const obj: Record<string, unknown> = {};
  for (const type in data) {
    for (const name in data[type]) {
      const domains = data[type][name].domains ?? [];
      for (const domain of domains) {
        obj[domain] = {
          type,
          name,
        };
      }
    }
  }

  return obj;
}

async function main() {
  // Get document, or throw exception on error
  try {
    const data = await fetch(
      'https://s3-eu-west-1.amazonaws.com/snowplow-hosted-assets/third-party/referer-parser/referers-latest.json'
    ).then((res) => res.json());

    fs.writeFileSync(
      path.resolve(__dirname, '../src/referrers/index.ts'),
      [
        '// This file is generated by the script get-referrers.ts',
        '',
        '// The data is fetch from snowplow-referer-parser https://github.com/snowplow-referer-parser/referer-parser',
        `// The orginal referers.yml is based on Piwik's SearchEngines.php and Socials.php, copyright 2012 Matthieu Aubry and available under the GNU General Public License v3.`,
        '',
        `const referrers: Record<string, { type: string, name: string }> = ${JSON.stringify(
          transform(data)
        )} as const;`,
        'export default referrers;',
      ].join('\n'),
      'utf-8'
    );
  } catch (e) {
    console.log(e);
  }
}

main();
