"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.importEVMTokens = exports.importTokenByChainId = void 0;
const path_1 = __importDefault(require("path"));
const promises_1 = __importDefault(require("fs/promises"));
const fs_1 = require("fs");
const fetch_1 = require("../../fetch");
const currencies_1 = require("../../../currencies");
const utils_1 = require("../../utils");
const chainNames = new Map();
const importTokenByChainId = async (outputDir, chainId) => {
    try {
        console.log(`importing chain with chainId: ${chainId}...`);
        const { tokens, hash } = await (0, fetch_1.fetchTokensFromCALService)({
            chainId,
            standard: chainId === 56 ? "bep20" : "erc20",
        }, [
            "blockchain_name",
            "id",
            "ticker",
            "decimals",
            "name",
            "live_signature",
            "contract_address",
            "delisted",
        ]);
        const { erc20, erc20Signatures } = (0, utils_1.getErc20DescriptorsAndSignatures)(tokens, chainId);
        const indexTsStringified = `import { ERC20Token } from "../../../types";
import erc20 from "./erc20.json";
export const tokens = erc20 as ERC20Token[];
export { default as signatures } from "./erc20-signatures.json";
${hash ? `export { default as hash } from "./erc20-hash.json";` : ""}
`;
        if (erc20?.length && erc20Signatures?.length) {
            const dirPath = path_1.default.join(outputDir, "evm", chainId.toString());
            if (!(0, fs_1.existsSync)(dirPath)) {
                await promises_1.default.mkdir(dirPath, { recursive: true });
            }
            const [currencyName] = erc20[0];
            chainNames.set(chainId, currencyName);
            await promises_1.default.writeFile(path_1.default.join(dirPath, "erc20.json"), JSON.stringify(erc20));
            if (hash) {
                await promises_1.default.writeFile(path_1.default.join(dirPath, "erc20-hash.json"), JSON.stringify(hash));
            }
            await promises_1.default.writeFile(path_1.default.join(dirPath, "erc20-signatures.json"), JSON.stringify(erc20Signatures.toString("base64")));
            await promises_1.default.writeFile(path_1.default.join(dirPath, "index.ts"), indexTsStringified);
            console.log(`importing chain with chainId: ${chainId} (${currencyName}) success`);
        }
    }
    catch (err) {
        console.error(err);
    }
};
exports.importTokenByChainId = importTokenByChainId;
const importEVMTokens = async (outputDir) => {
    console.log("Importing evm tokens...");
    const supportedChainIds = Object.values(currencies_1.cryptocurrenciesById)
        .filter(cryptocurrency => cryptocurrency.ethereumLikeInfo?.chainId)
        .map(cryptocurrency => cryptocurrency.ethereumLikeInfo.chainId)
        .sort((a, b) => a - b);
    await Promise.allSettled(supportedChainIds.map(chainId => (0, exports.importTokenByChainId)(outputDir, chainId)));
    const rootIndexStringified = `import { ERC20Token } from "../../types";

${supportedChainIds
        .map(chainId => chainNames.has(chainId)
        ? `import ${chainNames.get(chainId)}_tokens from "./${chainId}/erc20.json"`
        : null)
        .filter(Boolean)
        .join(";" + String.fromCharCode(10))};

${supportedChainIds
        .map(chainId => chainNames.has(chainId) &&
        (0, fs_1.existsSync)(path_1.default.join(outputDir, "evm", chainId.toString(), "erc20-hash.json"))
        ? `import ${chainNames.get(chainId)}_tokens_hash from "./${chainId}/erc20-hash.json"`
        : null)
        .filter(Boolean)
        .join(";" + String.fromCharCode(10))};

${supportedChainIds
        .map(chainId => chainNames.has(chainId)
        ? `import ${chainNames.get(chainId)}_signatures from "./${chainId}/erc20-signatures.json"`
        : null)
        .filter(Boolean)
        .join(";" + String.fromCharCode(10))};

export const tokens = {
${supportedChainIds
        .map(chainId => chainNames.has(chainId)
        ? `  ${chainId}: ${chainNames.get(chainId)}_tokens as ERC20Token[]`
        : null)
        .filter(Boolean)
        .join("," + String.fromCharCode(10))},
};

export const signatures = {
${supportedChainIds
        .map(chainId => chainNames.has(chainId) ? `  ${chainId}: ${chainNames.get(chainId)}_signatures` : null)
        .filter(Boolean)
        .join("," + String.fromCharCode(10))},
};

export const hashes = {
${supportedChainIds
        .map(chainId => chainNames.has(chainId) &&
        (0, fs_1.existsSync)(path_1.default.join(outputDir, "evm", chainId.toString(), "erc20-hash.json"))
        ? `  ${chainId}: ${chainNames.get(chainId)}_tokens_hash`
        : null)
        .filter(Boolean)
        .join("," + String.fromCharCode(10))},
};

export default {
  tokens,
  signatures,
  hashes,
};
`;
    await promises_1.default.writeFile(`${outputDir}/evm/index.ts`, rootIndexStringified);
    console.log("Importing evm tokens success");
};
exports.importEVMTokens = importEVMTokens;
//# sourceMappingURL=index.js.map