"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 Security Token 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.setDefaults = internal_2.createProcedureMethod({ getProcedureAndArgs: args => [internal_1.modifyCaDefaults, Object.assign({ ticker }, args)] }, context);
        this.setAgent = internal_2.createProcedureMethod({ getProcedureAndArgs: args => [internal_1.modifyCorporateActionsAgent, Object.assign({ ticker }, args)] }, context);
        this.removeAgent = internal_2.createProcedureMethod({ getProcedureAndArgs: () => [internal_1.removeCorporateActionsAgent, { ticker }] }, context);
        this.remove = 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(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: conversion_1.identityIdToString(storageKey.args[1]) }, context));
                }
            });
            return agentIdentities;
        });
    }
    /**
     * Retrieve default values for targets, global tax withholding percentage and per-identity tax withholding percentages.
     *
     *
     * @note These values are applied to every Corporate Action that is created until they are modified. Modifying these values
     *   does not impact existing Corporate Actions.
     *   When creating a Corporate Action, values passed explicitly will override these defaults
     */
    getDefaults() {
        return __awaiter(this, void 0, void 0, function* () {
            const { parent: { ticker }, context: { polymeshApi: { query: { corporateAction }, }, polymeshApi, }, context, } = this;
            const rawTicker = conversion_1.stringToTicker(ticker, context);
            const [targets, defaultTaxWithholding, taxWithholdings] = yield polymeshApi.queryMulti([
                [
                    corporateAction.defaultTargetIdentities,
                    rawTicker,
                ],
                [
                    corporateAction.defaultWithholdingTax,
                    rawTicker,
                ],
                [
                    corporateAction.didWithholdingTax,
                    rawTicker,
                ],
            ]);
            return {
                targets: conversion_1.targetIdentitiesToCorporateActionTargets(targets, context),
                defaultTaxWithholding: conversion_1.permillToBigNumber(defaultTaxWithholding),
                taxWithholdings: taxWithholdings.map(([identity, tax]) => ({
                    identity: new internal_1.Identity({ did: conversion_1.identityIdToString(identity) }, context),
                    percentage: conversion_1.permillToBigNumber(tax),
                })),
            };
        });
    }
}
exports.CorporateActions = CorporateActions;
//# sourceMappingURL=index.js.map