"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.Assets = void 0;
const internal_1 = require("./internal");
const types_1 = require("./types");
const conversion_1 = require("./utils/conversion");
const internal_2 = require("./utils/internal");
/**
 * Handles all Asset related functionality
 */
class Assets {
    /**
     * @hidden
     */
    constructor(context) {
        this.context = context;
        this.reserveTicker = (0, internal_2.createProcedureMethod)({
            getProcedureAndArgs: args => [internal_1.reserveTicker, args],
        }, context);
        this.claimClassicTicker = (0, internal_2.createProcedureMethod)({
            getProcedureAndArgs: args => [internal_1.claimClassicTicker, args],
        }, context);
        this.createAsset = (0, internal_2.createProcedureMethod)({
            getProcedureAndArgs: args => [internal_1.createAsset, Object.assign({ reservationRequired: false }, args)],
        }, context);
    }
    /**
     * Retrieve all the ticker reservations currently owned by an Identity. This doesn't include Assets that
     *   have already been launched
     *
     * @param args.owner - defaults to the signing Identity
     *
     * @note reservations with unreadable characters in their tickers will be left out
     */
    getTickerReservations(args) {
        return __awaiter(this, void 0, void 0, function* () {
            const { context: { polymeshApi: { query }, }, context, } = this;
            const did = yield (0, internal_2.getDid)(args === null || args === void 0 ? void 0 : args.owner, context);
            const entries = yield query.asset.assetOwnershipRelations.entries((0, conversion_1.stringToIdentityId)(did, context));
            return entries.reduce((result, [key, relation]) => {
                if (relation.isTickerOwned) {
                    const ticker = (0, conversion_1.tickerToString)(key.args[1]);
                    if ((0, internal_2.isPrintableAscii)(ticker)) {
                        return [...result, new internal_1.TickerReservation({ ticker }, context)];
                    }
                }
                return result;
            }, []);
        });
    }
    /**
     * Retrieve a Ticker Reservation
     *
     * @param args.ticker - Asset ticker
     */
    getTickerReservation(args) {
        const { ticker } = args;
        const { context } = this;
        return new internal_1.TickerReservation({ ticker }, context);
    }
    /**
     * Retrieve all of the Assets owned by an Identity
     *
     * @param args.owner - Identity representation or Identity ID as stored in the blockchain
     *
     * @note Assets with unreadable characters in their tickers will be left out
     */
    getAssets(args) {
        return __awaiter(this, void 0, void 0, function* () {
            const { context: { polymeshApi: { query }, }, context, } = this;
            const did = yield (0, internal_2.getDid)(args === null || args === void 0 ? void 0 : args.owner, context);
            const entries = yield query.asset.assetOwnershipRelations.entries((0, conversion_1.stringToIdentityId)(did, context));
            return entries.reduce((result, [key, relation]) => {
                if (relation.isAssetOwned) {
                    const ticker = (0, conversion_1.tickerToString)(key.args[1]);
                    if ((0, internal_2.isPrintableAscii)(ticker)) {
                        return [...result, new internal_1.Asset({ ticker }, context)];
                    }
                }
                return result;
            }, []);
        });
    }
    /**
     * Retrieve an Asset
     *
     * @param args.ticker - Asset ticker
     */
    getAsset(args) {
        return __awaiter(this, void 0, void 0, function* () {
            const { ticker } = args;
            const asset = new internal_1.Asset({ ticker }, this.context);
            const exists = yield asset.exists();
            if (!exists) {
                throw new internal_1.PolymeshError({
                    code: types_1.ErrorCode.DataUnavailable,
                    message: `There is no Asset with ticker "${ticker}"`,
                });
            }
            return asset;
        });
    }
}
exports.Assets = Assets;
//# sourceMappingURL=Assets.js.map