"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SET_CURRENT_TIME_DISCRIMINATOR = void 0;
exports.getSetCurrentTimeDiscriminatorBytes = getSetCurrentTimeDiscriminatorBytes;
exports.getSetCurrentTimeInstructionDataEncoder = getSetCurrentTimeInstructionDataEncoder;
exports.getSetCurrentTimeInstructionDataDecoder = getSetCurrentTimeInstructionDataDecoder;
exports.getSetCurrentTimeInstructionDataCodec = getSetCurrentTimeInstructionDataCodec;
exports.getSetCurrentTimeInstructionAsync = getSetCurrentTimeInstructionAsync;
exports.getSetCurrentTimeInstruction = getSetCurrentTimeInstruction;
exports.parseSetCurrentTimeInstruction = parseSetCurrentTimeInstruction;
var tslib_1 = require("tslib");
var kit_1 = require("@solana/kit");
var pdas_1 = require("../pdas");
var programs_1 = require("../programs");
var shared_1 = require("../shared");
exports.SET_CURRENT_TIME_DISCRIMINATOR = new Uint8Array([
    69, 100, 169, 193, 125, 0, 150, 69,
]);
function getSetCurrentTimeDiscriminatorBytes() {
    return (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8).encode(exports.SET_CURRENT_TIME_DISCRIMINATOR);
}
function getSetCurrentTimeInstructionDataEncoder() {
    return (0, kit_1.transformEncoder)((0, kit_1.getStructEncoder)([
        ["discriminator", (0, kit_1.fixEncoderSize)((0, kit_1.getBytesEncoder)(), 8)],
        ["newTime", (0, kit_1.getU64Encoder)()],
    ]), function (value) { return (tslib_1.__assign(tslib_1.__assign({}, value), { discriminator: exports.SET_CURRENT_TIME_DISCRIMINATOR })); });
}
function getSetCurrentTimeInstructionDataDecoder() {
    return (0, kit_1.getStructDecoder)([
        ["discriminator", (0, kit_1.fixDecoderSize)((0, kit_1.getBytesDecoder)(), 8)],
        ["newTime", (0, kit_1.getU64Decoder)()],
    ]);
}
function getSetCurrentTimeInstructionDataCodec() {
    return (0, kit_1.combineCodec)(getSetCurrentTimeInstructionDataEncoder(), getSetCurrentTimeInstructionDataDecoder());
}
function getSetCurrentTimeInstructionAsync(input, config) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var programAddress, originalAccounts, accounts, args, _a, getAccountMeta;
        var _b, _c, _d;
        return tslib_1.__generator(this, function (_e) {
            switch (_e.label) {
                case 0:
                    programAddress = (_b = config === null || config === void 0 ? void 0 : config.programAddress) !== null && _b !== void 0 ? _b : programs_1.SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS;
                    originalAccounts = {
                        state: { value: (_c = input.state) !== null && _c !== void 0 ? _c : null, isWritable: true },
                        signer: { value: (_d = input.signer) !== null && _d !== void 0 ? _d : null, isWritable: false },
                    };
                    accounts = originalAccounts;
                    args = tslib_1.__assign({}, input);
                    if (!!accounts.state.value) return [3, 2];
                    _a = accounts.state;
                    return [4, (0, pdas_1.findStatePda)()];
                case 1:
                    _a.value = _e.sent();
                    _e.label = 2;
                case 2:
                    getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, "programId");
                    return [2, Object.freeze({
                            accounts: [getAccountMeta(accounts.state), getAccountMeta(accounts.signer)],
                            data: getSetCurrentTimeInstructionDataEncoder().encode(args),
                            programAddress: programAddress,
                        })];
            }
        });
    });
}
function getSetCurrentTimeInstruction(input, config) {
    var _a, _b, _c;
    var programAddress = (_a = config === null || config === void 0 ? void 0 : config.programAddress) !== null && _a !== void 0 ? _a : programs_1.SPONSORED_CCTP_SRC_PERIPHERY_PROGRAM_ADDRESS;
    var originalAccounts = {
        state: { value: (_b = input.state) !== null && _b !== void 0 ? _b : null, isWritable: true },
        signer: { value: (_c = input.signer) !== null && _c !== void 0 ? _c : null, isWritable: false },
    };
    var accounts = originalAccounts;
    var args = tslib_1.__assign({}, input);
    var getAccountMeta = (0, shared_1.getAccountMetaFactory)(programAddress, "programId");
    return Object.freeze({
        accounts: [getAccountMeta(accounts.state), getAccountMeta(accounts.signer)],
        data: getSetCurrentTimeInstructionDataEncoder().encode(args),
        programAddress: programAddress,
    });
}
function parseSetCurrentTimeInstruction(instruction) {
    if (instruction.accounts.length < 2) {
        throw new Error("Not enough accounts");
    }
    var accountIndex = 0;
    var getNextAccount = function () {
        var accountMeta = instruction.accounts[accountIndex];
        accountIndex += 1;
        return accountMeta;
    };
    return {
        programAddress: instruction.programAddress,
        accounts: { state: getNextAccount(), signer: getNextAccount() },
        data: getSetCurrentTimeInstructionDataDecoder().decode(instruction.data),
    };
}
//# sourceMappingURL=setCurrentTime.js.map