"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 = internal_2.createProcedureMethod({
            getProcedureAndArgs: args => [
                internal_1.addTransferRestriction,
                Object.assign(Object.assign({}, args), { type: this.type, ticker }),
            ],
        }, context);
        this.setRestrictions = internal_2.createProcedureMethod({
            getProcedureAndArgs: args => [
                internal_1.setTransferRestrictions,
                Object.assign(Object.assign({}, args), { type: this.type, ticker }),
            ],
        }, context);
        this.removeRestrictions = 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, } = this;
            const rawTicker = conversion_1.stringToTicker(ticker, context);
            const activeTms = yield statistics.activeTransferManagers(rawTicker);
            const filteredTms = activeTms.filter(tm => {
                if (this.type === types_1.TransferRestrictionType.Count) {
                    return tm.isCountTransferManager;
                }
                return tm.isPercentageTransferManager;
            });
            const rawExemptedLists = yield Promise.all(filteredTms.map(tm => statistics.exemptEntities.entries([rawTicker, tm])));
            const restrictions = rawExemptedLists.map((list, index) => {
                const exemptedScopeIds = list.map(([{ args: [, scopeId], },]) => conversion_1.scopeIdToString(scopeId));
                const { value } = conversion_1.transferManagerToTransferRestriction(filteredTms[index]);
                let restriction;
                if (this.type === types_1.TransferRestrictionType.Count) {
                    restriction = {
                        count: value,
                    };
                }
                else {
                    restriction = {
                        percentage: value,
                    };
                }
                if (exemptedScopeIds.length) {
                    return Object.assign(Object.assign({}, restriction), { exemptedScopeIds });
                }
                return restriction;
            });
            const maxTransferManagers = conversion_1.u32ToBigNumber(consts.statistics.maxTransferManagersPerAsset);
            return {
                restrictions,
                availableSlots: maxTransferManagers.minus(activeTms.length),
            };
        });
    }
}
exports.TransferRestrictionBase = TransferRestrictionBase;
//# sourceMappingURL=TransferRestrictionBase.js.map