text: /* eslint-disable @typescript-eslint/no-var-requires */
  'use strict';
  
  const fs = require('fs');
  const path = require('path');
  const program = require('commander');
  const { sortBy } = require('lodash');
  const { getContractDeployments, NETWORKS } = require('./subgraphs/utils/network');
  
  program.action(async () => {
    const newLine = '\n';
    const readOnlyComment = `// this is a read only file generated by the codegen command.${newLine}`;
    const space = '\xa0';
    const doubleSpace = space + space;
    let escrowContractsContent = `${readOnlyComment}export let contracts = new Map<string, string>();${newLine}`;
  
    for (let i = 0; i < NETWORKS.length; i++) {
      const rewardEscrowContracts = sortBy(
        getContractDeployments('RewardEscrow', 0, Number.MAX_VALUE, NETWORKS[i]),
        'startBlock',
      );
  
      const rewardEscrowContract = rewardEscrowContracts[rewardEscrowContracts.length - 1];
      const synthetixEscrowContracts = sortBy(
        getContractDeployments('SynthetixEscrow', 0, Number.MAX_VALUE, NETWORKS[i]),
        'startBlock',
      );
  
      const synthetixEscrowContract = synthetixEscrowContracts[synthetixEscrowContracts.length - 1];
      const addressResolverContracts = sortBy(
        getContractDeployments('AddressResolver', 0, Number.MAX_VALUE, NETWORKS[i]),
        'startBlock',
      );
  
      const addressResolverContract = addressResolverContracts[addressResolverContracts.length - 1];
  
      escrowContractsContent += `contracts.set(${newLine}${doubleSpace}'rewardEscrow-${
        NETWORKS[i]
      }',${newLine}${doubleSpace}'${rewardEscrowContract.address.toLowerCase()}'${newLine});${newLine}`;
      escrowContractsContent += `contracts.set(${newLine}${doubleSpace}'escrow-${
        NETWORKS[i]
      }',${newLine}${doubleSpace}'${synthetixEscrowContract.address.toLowerCase()}'${newLine});${newLine}`;
      escrowContractsContent += `contracts.set(${newLine}${doubleSpace}'addressresolver-${
        NETWORKS[i]
      }',${newLine}${doubleSpace}'${addressResolverContract.address.toLowerCase()}'${newLine});${newLine}`;
    }
  
    escrowContractsContent += readOnlyComment;
    const targetFile = path.join(__dirname, './generated', 'contracts.ts');
    fs.writeFileSync(targetFile, escrowContractsContent, 'utf8');
  });
  
  program.parse(process.argv);
  