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());
    });
};
import { Buffer } from 'buffer';
import { PublicKey, } from '@solarti/web3.js';
import base58 from 'bs58';
import BN from 'bn.js';
import { GmaBuilder } from './GmaBuilder';
export class GpaBuilder {
    constructor(metaplex, programId) {
        /** The configs to use when fetching program accounts. */
        this.config = {};
        this.metaplex = metaplex;
        this.programId = programId;
    }
    mergeConfig(config) {
        this.config = Object.assign(Object.assign({}, this.config), config);
        return this;
    }
    slice(offset, length) {
        this.config.dataSlice = { offset, length };
        return this;
    }
    withoutData() {
        return this.slice(0, 0);
    }
    addFilter(...filters) {
        if (!this.config.filters) {
            this.config.filters = [];
        }
        this.config.filters.push(...filters);
        return this;
    }
    where(offset, bytes) {
        if (Buffer.isBuffer(bytes)) {
            bytes = base58.encode(bytes);
        }
        else if (typeof bytes === 'object' && 'toBase58' in bytes) {
            bytes = bytes.toBase58();
        }
        else if (BN.isBN(bytes)) {
            bytes = base58.encode(bytes.toArray());
        }
        else if (typeof bytes !== 'string') {
            bytes = base58.encode(new BN(bytes, 'le').toArray());
        }
        return this.addFilter({ memcmp: { offset, bytes } });
    }
    whereSize(dataSize) {
        return this.addFilter({ dataSize });
    }
    sortUsing(callback) {
        this.sortCallback = callback;
        return this;
    }
    get() {
        return __awaiter(this, void 0, void 0, function* () {
            const accounts = yield this.metaplex
                .rpc()
                .getProgramAccounts(this.programId, this.config);
            if (this.sortCallback) {
                accounts.sort(this.sortCallback);
            }
            return accounts;
        });
    }
    getAndMap(callback) {
        return __awaiter(this, void 0, void 0, function* () {
            return (yield this.get()).map(callback);
        });
    }
    getPublicKeys() {
        return __awaiter(this, void 0, void 0, function* () {
            return this.getAndMap((account) => account.publicKey);
        });
    }
    getDataAsPublicKeys() {
        return __awaiter(this, void 0, void 0, function* () {
            // TODO(loris): Throw a custom Metaplex error if the data is not a public key.
            return this.getAndMap((account) => new PublicKey(account.data));
        });
    }
    getMultipleAccounts(callback, options) {
        return __awaiter(this, void 0, void 0, function* () {
            // TODO(loris): Throw a custom Metaplex error if the data is not a public key.
            const cb = callback !== null && callback !== void 0 ? callback : ((account) => new PublicKey(account.data));
            return new GmaBuilder(this.metaplex, yield this.getAndMap(cb), options);
        });
    }
}
//# sourceMappingURL=GpaBuilder.js.map