"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.Offerings = void 0;
const lodash_1 = require("lodash");
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 Offering related functionality
 */
class Offerings extends internal_1.Namespace {
    /**
     * @hidden
     */
    constructor(parent, context) {
        super(parent, context);
        const { ticker } = parent;
        this.launch = internal_2.createProcedureMethod({ getProcedureAndArgs: args => [internal_1.launchOffering, Object.assign({ ticker }, args)] }, context);
    }
    /**
     * Retrieve a single Offering associated to this Asset by its ID
     *
     * @throws if there is no Offering with the passed ID
     */
    getOne(args) {
        return __awaiter(this, void 0, void 0, function* () {
            const { parent: { ticker }, context, } = this;
            const { id } = args;
            const offering = new internal_1.Offering({ ticker, id }, context);
            const exists = yield offering.exists();
            if (!exists) {
                throw new internal_1.PolymeshError({
                    code: types_1.ErrorCode.DataUnavailable,
                    message: 'The Offering does not exist',
                });
            }
            return offering;
        });
    }
    /**
     * Retrieve all of the Asset's Offerings and their details. Can be filtered using parameters
     *
     * @param opts.status - status of the Offerings to fetch. If defined, only Offerings that have all passed statuses will be returned
     */
    get(opts = {}) {
        return __awaiter(this, void 0, void 0, function* () {
            const { parent: { ticker }, context: { polymeshApi: { query: { sto }, }, }, context, } = this;
            const { status: { timing: timingFilter, balance: balanceFilter, sale: saleFilter } = {} } = opts;
            const rawTicker = conversion_1.stringToTicker(ticker, context);
            const [fundraiserEntries, nameEntries] = yield Promise.all([
                sto.fundraisers.entries(rawTicker),
                sto.fundraiserNames.entries(rawTicker),
            ]);
            const offerings = fundraiserEntries.map(([{ args: [, rawFundraiserId], }, fundraiser,]) => {
                const id = conversion_1.u64ToBigNumber(rawFundraiserId);
                const [[, name]] = lodash_1.remove(nameEntries, ([{ args: [, rawId], },]) => conversion_1.u64ToBigNumber(rawId).eq(id));
                return {
                    offering: new internal_1.Offering({ id, ticker }, context),
                    details: conversion_1.fundraiserToOfferingDetails(fundraiser.unwrap(), name, context),
                };
            });
            return offerings.filter(({ details: { status: { timing, sale, balance }, }, }) => (!timingFilter || timingFilter === timing) &&
                (!saleFilter || saleFilter === sale) &&
                (!balanceFilter || balanceFilter === balance));
        });
    }
}
exports.Offerings = Offerings;
//# sourceMappingURL=Offerings.js.map