"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.expectSome = expectSome;
exports.expectAddress = expectAddress;
exports.expectProgramDerivedAddress = expectProgramDerivedAddress;
exports.expectTransactionSigner = expectTransactionSigner;
exports.getAccountMetaFactory = getAccountMetaFactory;
exports.isTransactionSigner = isTransactionSigner;
var tslib_1 = require("tslib");
var kit_1 = require("@solana/kit");
function expectSome(value) {
    if (value === null || value === undefined) {
        throw new Error("Expected a value but received null or undefined.");
    }
    return value;
}
function expectAddress(value) {
    if (!value) {
        throw new Error("Expected a Address.");
    }
    if (typeof value === "object" && "address" in value) {
        return value.address;
    }
    if (Array.isArray(value)) {
        return value[0];
    }
    return value;
}
function expectProgramDerivedAddress(value) {
    if (!value || !Array.isArray(value) || !(0, kit_1.isProgramDerivedAddress)(value)) {
        throw new Error("Expected a ProgramDerivedAddress.");
    }
    return value;
}
function expectTransactionSigner(value) {
    if (!value || !isTransactionSigner(value)) {
        throw new Error("Expected a TransactionSigner.");
    }
    return value;
}
function getAccountMetaFactory(programAddress, optionalAccountStrategy) {
    return function (account) {
        if (!account.value) {
            if (optionalAccountStrategy === "omitted")
                return;
            return Object.freeze({
                address: programAddress,
                role: kit_1.AccountRole.READONLY,
            });
        }
        var writableRole = account.isWritable
            ? kit_1.AccountRole.WRITABLE
            : kit_1.AccountRole.READONLY;
        return Object.freeze(tslib_1.__assign({ address: expectAddress(account.value), role: isTransactionSigner(account.value)
                ? (0, kit_1.upgradeRoleToSigner)(writableRole)
                : writableRole }, (isTransactionSigner(account.value) ? { signer: account.value } : {})));
    };
}
function isTransactionSigner(value) {
    return (!!value &&
        typeof value === "object" &&
        "address" in value &&
        (0, kit_1.isTransactionSigner)(value));
}
//# sourceMappingURL=index.js.map