"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.CorporateActions = void 0;
const internal_1 = require("../../../../internal");
const conversion_1 = require("../../../../utils/conversion");
const internal_2 = require("../../../../utils/internal");
const Distributions_1 = require("./Distributions");
/**
 * Handles all Asset Corporate Actions related functionality
 */
class CorporateActions extends internal_1.Namespace {
    /**
     * @hidden
     */
    constructor(parent, context) {
        super(parent, context);
        const { ticker } = parent;
        this.distributions = new Distributions_1.Distributions(parent, context);
        this.setDefaultConfig = (0, internal_2.createProcedureMethod)({ getProcedureAndArgs: args => [internal_1.modifyCaDefaultConfig, Object.assign({ ticker }, args)] }, context);
        this.setAgent = (0, internal_2.createProcedureMethod)({ getProcedureAndArgs: args => [internal_1.modifyCorporateActionsAgent, Object.assign({ ticker }, args)] }, context);
        this.removeAgent = (0, internal_2.createProcedureMethod)({ getProcedureAndArgs: () => [internal_1.removeCorporateActionsAgent, { ticker }], voidArgs: true }, context);
        this.remove = (0, internal_2.createProcedureMethod)({ getProcedureAndArgs: args => [internal_1.removeCorporateAction, Object.assign({ ticker }, args)] }, context);
    }
    /**
     * Retrieve a list of agent Identities
     */
    getAgents() {
        return __awaiter(this, void 0, void 0, function* () {
            const { context: { polymeshApi: { query: { externalAgents }, }, }, parent: { ticker }, context, } = this;
            const groupOfAgent = yield externalAgents.groupOfAgent.entries((0, conversion_1.stringToTicker)(ticker, context));
            const agentIdentities = [];
            groupOfAgent.forEach(([storageKey, agentGroup]) => {
                const rawAgentGroup = agentGroup.unwrap();
                if (rawAgentGroup.isPolymeshV1CAA) {
                    agentIdentities.push(new internal_1.Identity({ did: (0, conversion_1.identityIdToString)(storageKey.args[1]) }, context));
                }
            });
            return agentIdentities;
        });
    }
    /**
     * Retrieve default config comprising of targets, global tax withholding percentage and per-Identity tax withholding percentages.
     *
     *
     * @note This config is applied to every Corporate Action that is created until they are modified. Modifying the default config
     *   does not impact existing Corporate Actions.
     *   When creating a Corporate Action, values passed explicitly will override this default config
     */
    getDefaultConfig() {
        return __awaiter(this, void 0, void 0, function* () {
            const { parent: { ticker }, context: { polymeshApi: { query: { corporateAction }, }, polymeshApi, }, context, } = this;
            const rawTicker = (0, conversion_1.stringToTicker)(ticker, context);
            const [targets, defaultTaxWithholding, taxWithholdings] = yield polymeshApi.queryMulti([
                [
                    corporateAction.defaultTargetIdentities,
                    rawTicker,
                ],
                [
                    corporateAction.defaultWithholdingTax,
                    rawTicker,
                ],
                [corporateAction.didWithholdingTax, rawTicker],
            ]);
            return {
                targets: (0, conversion_1.targetIdentitiesToCorporateActionTargets)(targets, context),
                defaultTaxWithholding: (0, conversion_1.permillToBigNumber)(defaultTaxWithholding),
                taxWithholdings: taxWithholdings.map(([identity, tax]) => ({
                    identity: new internal_1.Identity({ did: (0, conversion_1.identityIdToString)(identity) }, context),
                    percentage: (0, conversion_1.permillToBigNumber)(tax),
                })),
            };
        });
    }
}
exports.CorporateActions = CorporateActions;
//# sourceMappingURL=index.js.map