import { MissingGpaBuilderError, ProgramNotRecognizedError } from '../../errors';
/**
 * @group Modules
 */
export class ProgramClient {
    constructor(metaplex) {
        this.metaplex = metaplex;
        this.programs = [];
    }
    register(program) {
        this.programs.unshift(program);
    }
    all(overrides = []) {
        return [...overrides, ...this.programs];
    }
    allForCluster(cluster, overrides = []) {
        return this.all(overrides).filter((program) => {
            var _a, _b;
            return (_b = (_a = program.clusterFilter) === null || _a === void 0 ? void 0 : _a.call(program, cluster)) !== null && _b !== void 0 ? _b : true;
        });
    }
    allForCurrentCluster(overrides = []) {
        return this.allForCluster(this.metaplex.cluster, overrides);
    }
    get(nameOrAddress, overrides = []) {
        const programs = this.allForCurrentCluster(overrides);
        const program = typeof nameOrAddress === 'string'
            ? programs.find((program) => program.name === nameOrAddress)
            : programs.find((program) => program.address.equals(nameOrAddress));
        if (!program) {
            throw new ProgramNotRecognizedError(nameOrAddress, this.metaplex.cluster);
        }
        return program;
    }
    getGpaBuilder(nameOrAddress, overrides = []) {
        const program = this.get(nameOrAddress, overrides);
        if (!program.gpaResolver) {
            throw new MissingGpaBuilderError(program);
        }
        return program.gpaResolver(this.metaplex);
    }
}
//# sourceMappingURL=ProgramClient.js.map