"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.createCheckpointSchedule = exports.getAuthorization = exports.prepareCreateCheckpointSchedule = exports.createCheckpointScheduleResolver = 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 createCheckpointScheduleResolver = (ticker, context) => (receipt) => {
    const [{ data }] = internal_2.filterEventRecords(receipt, 'checkpoint', 'ScheduleCreated');
    const scheduleParams = conversion_1.storedScheduleToCheckpointScheduleParams(data[2]);
    return new internal_1.CheckpointSchedule(Object.assign({ ticker }, scheduleParams), context);
};
exports.createCheckpointScheduleResolver = createCheckpointScheduleResolver;
/**
 * @hidden
 */
function prepareCreateCheckpointSchedule(args) {
    return __awaiter(this, void 0, void 0, function* () {
        const { context } = this;
        const { ticker, start, period, repetitions } = args;
        const now = new Date();
        if (start && start < now) {
            throw new internal_1.PolymeshError({
                code: types_1.ErrorCode.ValidationError,
                message: 'Schedule start date must be in the future',
            });
        }
        const rawTicker = conversion_1.stringToTicker(ticker, context);
        const rawSchedule = conversion_1.scheduleSpecToMeshScheduleSpec({ start, period, repetitions }, context);
        const [schedule] = this.addTransaction(context.polymeshApi.tx.checkpoint.createSchedule, { resolvers: [exports.createCheckpointScheduleResolver(ticker, context)] }, rawTicker, rawSchedule);
        return schedule;
    });
}
exports.prepareCreateCheckpointSchedule = prepareCreateCheckpointSchedule;
/**
 * @hidden
 */
function getAuthorization({ ticker }) {
    const { context } = this;
    return {
        permissions: {
            transactions: [types_1.TxTags.checkpoint.CreateSchedule],
            tokens: [new internal_1.SecurityToken({ ticker }, context)],
            portfolios: [],
        },
    };
}
exports.getAuthorization = getAuthorization;
/**
 * @hidden
 */
const createCheckpointSchedule = () => new internal_1.Procedure(prepareCreateCheckpointSchedule, getAuthorization);
exports.createCheckpointSchedule = createCheckpointSchedule;
//# sourceMappingURL=createCheckpointSchedule.js.map