"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());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Schedules = void 0;
const bluebird_1 = __importDefault(require("bluebird"));
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 Checkpoint Schedules related functionality
 */
class Schedules extends internal_1.Namespace {
    /**
     * @hidden
     */
    constructor(parent, context) {
        super(parent, context);
        const { ticker } = parent;
        this.create = internal_2.createProcedureMethod({ getProcedureAndArgs: args => [internal_1.createCheckpointSchedule, Object.assign({ ticker }, args)] }, context);
        this.remove = internal_2.createProcedureMethod({ getProcedureAndArgs: args => [internal_1.removeCheckpointSchedule, Object.assign({ ticker }, args)] }, context);
    }
    /**
     * Retrieve a single Checkpoint Schedule associated to this Security Token by its ID
     *
     * @throws if there is no Schedule with the passed ID
     */
    getOne({ id }) {
        return __awaiter(this, void 0, void 0, function* () {
            const schedules = yield this.get();
            const result = schedules.find(({ schedule }) => schedule.id.eq(id));
            if (!result) {
                throw new internal_1.PolymeshError({
                    code: types_1.ErrorCode.DataUnavailable,
                    message: 'The Schedule does not exist',
                });
            }
            return result;
        });
    }
    /**
     * Retrieve all active Checkpoint Schedules
     */
    get() {
        return __awaiter(this, void 0, void 0, function* () {
            const { parent: { ticker }, context: { polymeshApi: { query: { checkpoint }, }, }, context, } = this;
            const rawTicker = conversion_1.stringToTicker(ticker, context);
            const rawSchedules = yield checkpoint.schedules(rawTicker);
            return bluebird_1.default.map(rawSchedules, (rawSchedule) => __awaiter(this, void 0, void 0, function* () {
                const scheduleParams = conversion_1.storedScheduleToCheckpointScheduleParams(rawSchedule);
                const schedule = new internal_1.CheckpointSchedule(Object.assign(Object.assign({}, scheduleParams), { ticker }), context);
                const { remaining: remainingCheckpoints, nextCheckpointDate } = scheduleParams;
                return {
                    schedule,
                    details: {
                        remainingCheckpoints,
                        nextCheckpointDate,
                    },
                };
            }));
        });
    }
    /**
     * Calculate an abstract measure of the complexity of a given Calendar Period
     */
    complexityOf(period) {
        return internal_2.periodComplexity(period);
    }
    /**
     * Calculate the sum of the complexity of all current Checkpoint Schedules for this Security Token.
     *   The number cannot exceed the Token's maximum complexity (obtained via [[maxComplexity]])
     */
    currentComplexity() {
        return __awaiter(this, void 0, void 0, function* () {
            const schedules = yield this.get();
            return schedules.reduce((prev, next) => prev + next.schedule.complexity, 0);
        });
    }
    /**
     * Retrieve the maximum allowed Schedule complexity for this Security Token
     */
    maxComplexity() {
        return __awaiter(this, void 0, void 0, function* () {
            const { context } = this;
            const complexity = yield context.polymeshApi.query.checkpoint.schedulesMaxComplexity();
            return conversion_1.u64ToBigNumber(complexity).toNumber();
        });
    }
}
exports.Schedules = Schedules;
//# sourceMappingURL=Schedules.js.map