"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DefaultAssetRegistryAPI = void 0;
const utils_1 = require("../utils");
class DefaultAssetRegistryAPI {
    constructor(api) {
        this.api = api;
    }
    // not private for easier testing
    static metadataToCurrency(metadata) {
        const symbol = utils_1.decodeBytesAsString(metadata.symbol);
        const name = utils_1.decodeBytesAsString(metadata.name);
        const DynamicUnit = {
            atomic: 0,
        };
        DynamicUnit[symbol] = metadata.decimals.toNumber();
        return {
            name: name,
            base: DynamicUnit[symbol],
            rawBase: DynamicUnit.atomic,
            units: DynamicUnit,
            ticker: symbol,
        };
    }
    // wrapped call for easier mocking in tests
    getAssetRegistryEntries() {
        return __awaiter(this, void 0, void 0, function* () {
            return yield this.api.query.assetRegistry.metadata.entries();
        });
    }
    /**
     * Static method to grab onle metadata from entries provided by the asset registry.
     * Ignores entries with no metadata (ie. `Option.isSome !== true`).
     * @param entries The entries from the asset registry.
     * @returns A list of metadata.
     */
    static extractMetadataFromEntries(entries) {
        return entries.filter(([, metadata]) => metadata.isSome).map(([, metadata]) => metadata.unwrap());
    }
    getForeignAssetsAsCurrencies() {
        return __awaiter(this, void 0, void 0, function* () {
            const entries = yield this.getAssetRegistryEntries();
            return DefaultAssetRegistryAPI.extractMetadataFromEntries(entries).map(DefaultAssetRegistryAPI.metadataToCurrency);
        });
    }
}
exports.DefaultAssetRegistryAPI = DefaultAssetRegistryAPI;
//# sourceMappingURL=asset-registry.js.map