"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.Distributions = 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 Security Token Distributions related functionality
 */
class Distributions extends internal_1.Namespace {
    /**
     * @hidden
     */
    constructor(parent, context) {
        super(parent, context);
        const { ticker } = parent;
        this.configureDividendDistribution = internal_2.createProcedureMethod({ getProcedureAndArgs: args => [internal_1.configureDividendDistribution, Object.assign({ ticker }, args)] }, context);
    }
    /**
     * Retrieve a single Dividend Distribution associated to this Security Token by its ID
     *
     * @throws if there is no Distribution with the passed ID
     */
    getOne(args) {
        return __awaiter(this, void 0, void 0, function* () {
            const { parent: { ticker }, context: { polymeshApi: { query }, }, context, } = this;
            const { id } = args;
            const rawTicker = conversion_1.stringToTicker(ticker, context);
            const rawLocalId = conversion_1.numberToU32(id, context);
            const rawCaId = conversion_1.corporateActionIdentifierToCaId({ ticker, localId: id }, context);
            const [corporateAction, capitalDistribution, details] = yield Promise.all([
                query.corporateAction.corporateActions(rawTicker, rawLocalId),
                query.capitalDistribution.distributions(rawCaId),
                query.corporateAction.details(rawCaId),
            ]);
            if (corporateAction.isNone || capitalDistribution.isNone) {
                throw new internal_1.PolymeshError({
                    code: types_1.ErrorCode.DataUnavailable,
                    message: 'The Dividend Distribution does not exist',
                });
            }
            const dist = capitalDistribution.unwrap();
            const distribution = new internal_1.DividendDistribution(Object.assign(Object.assign({ id,
                ticker }, conversion_1.meshCorporateActionToCorporateActionParams(corporateAction.unwrap(), details, context)), conversion_1.distributionToDividendDistributionParams(dist, context)), context);
            const { reclaimed, remaining } = dist;
            return {
                distribution,
                details: {
                    remainingFunds: conversion_1.balanceToBigNumber(remaining),
                    fundsReclaimed: conversion_1.boolToBoolean(reclaimed),
                },
            };
        });
    }
    /**
     * Retrieve all Dividend Distributions associated to this Security Token, along with their details
     */
    get() {
        const { parent, context } = this;
        return context.getDividendDistributionsForTokens({ tokens: [parent] });
    }
}
exports.Distributions = Distributions;
//# sourceMappingURL=Distributions.js.map