"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.TransferRestrictionBase = void 0;
const internal_1 = require("../../../../internal");
const types_1 = require("../../../../types");
const conversion_1 = require("../../../../utils/conversion");
const internal_2 = require("../../../../utils/internal");
/**
 * Base class for managing Transfer Restrictions
 */
class TransferRestrictionBase extends internal_1.Namespace {
    /**
     * @hidden
     */
    constructor(parent, context) {
        super(parent, context);
        const { ticker } = parent;
        this.addRestriction = (0, internal_2.createProcedureMethod)({
            getProcedureAndArgs: args => [
                internal_1.addTransferRestriction,
                Object.assign(Object.assign({}, args), { type: this.type, ticker }),
            ],
        }, context);
        this.setRestrictions = (0, internal_2.createProcedureMethod)({
            getProcedureAndArgs: args => [
                internal_1.setTransferRestrictions,
                Object.assign(Object.assign({}, args), { type: this.type, ticker }),
            ],
        }, context);
        this.removeRestrictions = (0, internal_2.createProcedureMethod)({
            getProcedureAndArgs: () => [
                internal_1.setTransferRestrictions,
                {
                    restrictions: [],
                    type: this.type,
                    ticker,
                },
            ],
            voidArgs: true,
        }, context);
    }
    /**
     * Retrieve all active Transfer Restrictions of the corresponding type
     *
     * @note there is a maximum number of restrictions allowed across all types.
     *   The `availableSlots` property of the result represents how many more restrictions can be added
     *   before reaching that limit
     */
    get() {
        return __awaiter(this, void 0, void 0, function* () {
            const { parent: { ticker }, context: { polymeshApi: { query: { statistics }, consts, }, }, context, type, } = this;
            const tickerKey = (0, conversion_1.stringToTickerKey)(ticker, context);
            const complianceRules = yield statistics.assetTransferCompliances(tickerKey);
            const filteredRequirements = complianceRules.requirements.filter(requirement => {
                if (type === types_1.TransferRestrictionType.Count) {
                    return requirement.isMaxInvestorCount;
                }
                return requirement.isMaxInvestorOwnership;
            });
            const rawExemptedLists = yield Promise.all(filteredRequirements.map(() => statistics.transferConditionExemptEntities.entries({ asset: tickerKey })));
            const restrictions = rawExemptedLists.map((list, index) => {
                const exemptedIds = list.map(([{ args: [, scopeId], },]) => (0, conversion_1.scopeIdToString)(scopeId) // `ScopeId` and `IdentityId` are the same type, so this is fine
                );
                const { value } = (0, conversion_1.transferConditionToTransferRestriction)(filteredRequirements[index]);
                let restriction;
                if (type === types_1.TransferRestrictionType.Count) {
                    restriction = {
                        count: value,
                    };
                }
                else {
                    restriction = {
                        percentage: value,
                    };
                }
                if (exemptedIds.length) {
                    return Object.assign(Object.assign({}, restriction), { exemptedIds });
                }
                return restriction;
            });
            const maxTransferConditions = (0, conversion_1.u32ToBigNumber)(consts.statistics.maxTransferConditionsPerAsset);
            return {
                restrictions: restrictions,
                availableSlots: maxTransferConditions.minus(restrictions.length),
            };
        });
    }
}
exports.TransferRestrictionBase = TransferRestrictionBase;
//# sourceMappingURL=TransferRestrictionBase.js.map