"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.removeCheckpointSchedule = exports.getAuthorization = exports.prepareRemoveCheckpointSchedule = void 0;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const internal_1 = require("../../internal");
const types_1 = require("../../types");
const conversion_1 = require("../../utils/conversion");
/**
 * @hidden
 */
function prepareRemoveCheckpointSchedule(args) {
    return __awaiter(this, void 0, void 0, function* () {
        const { context, context: { polymeshApi: { tx, query }, }, } = this;
        const { ticker, schedule } = args;
        const id = schedule instanceof bignumber_js_1.default ? schedule : schedule.id;
        const rawTicker = conversion_1.stringToTicker(ticker, context);
        const rawSchedules = yield query.checkpoint.schedules(rawTicker);
        const exists = rawSchedules.find(({ id: scheduleId }) => conversion_1.u64ToBigNumber(scheduleId).eq(id));
        if (!exists) {
            throw new internal_1.PolymeshError({
                code: types_1.ErrorCode.DataUnavailable,
                message: 'Schedule was not found. It may have been removed or expired',
            });
        }
        const rawScheduleId = conversion_1.bigNumberToU64(id, context);
        const scheduleRefCount = yield query.checkpoint.scheduleRefCount(rawTicker, rawScheduleId);
        const referenceCount = conversion_1.u32ToBigNumber(scheduleRefCount);
        if (referenceCount.gt(0)) {
            throw new internal_1.PolymeshError({
                code: types_1.ErrorCode.EntityInUse,
                message: 'This Schedule is being referenced by other Entities. It cannot be removed',
                data: {
                    referenceCount,
                },
            });
        }
        this.addTransaction({
            transaction: tx.checkpoint.removeSchedule,
            args: [rawTicker, rawScheduleId],
        });
    });
}
exports.prepareRemoveCheckpointSchedule = prepareRemoveCheckpointSchedule;
/**
 * @hidden
 */
function getAuthorization({ ticker }) {
    const { context } = this;
    return {
        permissions: {
            transactions: [types_1.TxTags.checkpoint.RemoveSchedule],
            assets: [new internal_1.Asset({ ticker }, context)],
            portfolios: [],
        },
    };
}
exports.getAuthorization = getAuthorization;
/**
 * @hidden
 */
const removeCheckpointSchedule = () => new internal_1.Procedure(prepareRemoveCheckpointSchedule, getAuthorization);
exports.removeCheckpointSchedule = removeCheckpointSchedule;
//# sourceMappingURL=removeCheckpointSchedule.js.map