"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Executor = void 0;
const socket_connection_js_1 = require("./socket-connection.js");
const generateUUID = () => {
    if (globalThis.crypto && globalThis.crypto.randomUUID) {
        return globalThis.crypto.randomUUID();
    }
    // A simple fallback for environments without crypto.randomUUID
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
        const r = (Math.random() * 16) | 0;
        const v = c === 'x' ? r : (r & 0x3) | 0x8;
        return v.toString(16);
    });
};
class Executor {
    #baseUrl;
    #apiKey;
    #socketConnection;
    #baseId;
    #transactionId;
    constructor(baseUrl, apiKey, baseId, transactionId, socketConnection) {
        this.#baseUrl = baseUrl;
        this.#apiKey = apiKey;
        this.#baseId = baseId;
        this.#transactionId = transactionId;
        this.#socketConnection =
            socketConnection ??
                new socket_connection_js_1.SocketConnection({
                    baseUrl,
                    apiKey,
                    baseId,
                    clientId: generateUUID(),
                });
    }
    getApiKey() {
        return this.#apiKey;
    }
    getBaseId() {
        return this.#baseId;
    }
    get isInTransaction() {
        return !!this.#transactionId;
    }
    withTransactionId(transactionId) {
        return new Executor(this.#baseUrl, this.#apiKey, this.#baseId, transactionId, this.#socketConnection);
    }
    async execute(builder) {
        const { query, variables } = builder.compile();
        return this.rawRequest(query, variables);
    }
    async rawRequest(query, variables, headers) {
        return this.#socketConnection.rawRequest(query, variables, headers, this.#transactionId);
    }
    subscribe(builders, callback) {
        const metadatas = builders.map(b => b._prepareMetadata());
        return this.#socketConnection.subscribe(metadatas, callback);
    }
}
exports.Executor = Executor;
//# sourceMappingURL=executor.js.map