"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.createCheckpoint = exports.getAuthorization = exports.prepareCreateCheckpoint = exports.createCheckpointResolver = void 0;
const internal_1 = require("../../internal");
const types_1 = require("../../types");
const conversion_1 = require("../../utils/conversion");
const internal_2 = require("../../utils/internal");
/**
 * @hidden
 */
const createCheckpointResolver = (ticker, context) => (receipt) => {
    const [{ data }] = internal_2.filterEventRecords(receipt, 'checkpoint', 'CheckpointCreated');
    const id = conversion_1.u64ToBigNumber(data[2]);
    return new internal_1.Checkpoint({ ticker, id }, context);
};
exports.createCheckpointResolver = createCheckpointResolver;
/**
 * @hidden
 */
function prepareCreateCheckpoint(args) {
    return __awaiter(this, void 0, void 0, function* () {
        const { context } = this;
        const { ticker } = args;
        const rawTicker = conversion_1.stringToTicker(ticker, context);
        const [checkpoint] = this.addTransaction(context.polymeshApi.tx.checkpoint.createCheckpoint, {
            resolvers: [exports.createCheckpointResolver(ticker, context)],
        }, rawTicker);
        return checkpoint;
    });
}
exports.prepareCreateCheckpoint = prepareCreateCheckpoint;
/**
 * @hidden
 */
function getAuthorization({ ticker }) {
    return {
        permissions: {
            transactions: [types_1.TxTags.checkpoint.CreateCheckpoint],
            tokens: [new internal_1.SecurityToken({ ticker }, this.context)],
            portfolios: [],
        },
    };
}
exports.getAuthorization = getAuthorization;
/**
 * @hidden
 */
const createCheckpoint = () => new internal_1.Procedure(prepareCreateCheckpoint, getAuthorization);
exports.createCheckpoint = createCheckpoint;
//# sourceMappingURL=createCheckpoint.js.map