"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.Checkpoints = void 0;
const internal_1 = require("../../../../internal");
const types_1 = require("../../../../types");
const utils_1 = require("../../../../types/utils");
const conversion_1 = require("../../../../utils/conversion");
const internal_2 = require("../../../../utils/internal");
const Schedules_1 = require("./Schedules");
/**
 * Handles all Security Token Checkpoints related functionality
 */
class Checkpoints extends internal_1.Namespace {
    /**
     * @hidden
     */
    constructor(parent, context) {
        super(parent, context);
        const { ticker } = parent;
        this.create = internal_2.createProcedureMethod({ getProcedureAndArgs: () => [internal_1.createCheckpoint, { ticker }] }, context);
        this.schedules = new Schedules_1.Schedules(parent, context);
    }
    /**
     * Retrieve a single Checkpoint for this Security Token by its ID
     *
     * @throws if there is no Checkpoint with the passed ID
     */
    getOne(args) {
        return __awaiter(this, void 0, void 0, function* () {
            const { parent: { ticker }, context, } = this;
            const checkpoint = new internal_1.Checkpoint({ id: args.id, ticker }, context);
            const exists = yield checkpoint.exists();
            if (!exists) {
                throw new internal_1.PolymeshError({
                    code: types_1.ErrorCode.DataUnavailable,
                    message: 'The Checkpoint does not exist',
                });
            }
            return checkpoint;
        });
    }
    /**
     * Retrieve all Checkpoints created on this Security Token, together with their corresponding creation Date and Total Supply
     *
     * @note supports pagination
     */
    get(paginationOpts) {
        return __awaiter(this, void 0, void 0, function* () {
            const { parent: { ticker }, context, context: { polymeshApi: { query: { checkpoint: checkpointQuery }, }, }, } = this;
            const rawTicker = conversion_1.stringToTicker(ticker, context);
            const { entries, lastKey: next } = yield internal_2.requestPaginated(checkpointQuery.totalSupply, {
                arg: rawTicker,
                paginationOpts,
            });
            const checkpointsMultiParams = [];
            const checkpoints = [];
            entries.forEach(([{ args: [, id], }, balance,]) => {
                checkpointsMultiParams.push(utils_1.tuple(rawTicker, id));
                checkpoints.push({
                    checkpoint: new internal_1.Checkpoint({ id: conversion_1.u64ToBigNumber(id), ticker }, context),
                    totalSupply: conversion_1.balanceToBigNumber(balance),
                });
            });
            const timestamps = yield checkpointQuery.timestamps.multi(checkpointsMultiParams);
            const data = timestamps.map((moment, i) => {
                const { totalSupply, checkpoint } = checkpoints[i];
                return {
                    checkpoint,
                    totalSupply,
                    createdAt: conversion_1.momentToDate(moment),
                };
            });
            return {
                data,
                next,
            };
        });
    }
}
exports.Checkpoints = Checkpoints;
//# sourceMappingURL=index.js.map