"use strict";
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());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.IndexingStatusResolver = void 0;
const isomorphic_fetch_1 = __importDefault(require("isomorphic-fetch"));
const core_1 = require("@urql/core");
const common_ts_1 = require("@graphprotocol/common-ts");
const errors_1 = require("./errors");
class IndexingStatusResolver {
    constructor(options) {
        this.logger = options.logger.child({ component: 'IndexingStatusFetcher' });
        this.statuses = core_1.createClient({
            url: options.statusEndpoint,
            fetch: isomorphic_fetch_1.default,
            requestPolicy: 'network-only',
        });
    }
    indexingStatus(deployments) {
        return __awaiter(this, void 0, void 0, function* () {
            const indexingStatusesQueryBody = `
      subgraphDeployment: subgraph
      synced
      health
      fatalError {
        handler
        message
      }
      node
      chains {
        network
        ... on EthereumIndexingStatus {
          latestBlock {
            number
            hash
          }
          chainHeadBlock {
            number
            hash
          }
          earliestBlock {
            number
            hash
          }
        }
      }`;
            const query = deployments.length > 0
                ? `query indexingStatus($deployments: [String!]!) {
            indexingStatuses(subgraphs: $deployments) {
              ${indexingStatusesQueryBody}
            }
          }`
                : `query indexingStatus {
            indexingStatuses {
              ${indexingStatusesQueryBody}
            }
          }`;
            try {
                const result = yield this.statuses
                    .query(query, { deployments: deployments.map((id) => id.ipfsHash) })
                    .toPromise();
                return (result.data.indexingStatuses
                    // eslint-disable-next-line @typescript-eslint/no-explicit-any
                    .map((status) => (Object.assign(Object.assign({}, status), { subgraphDeployment: new common_ts_1.SubgraphDeploymentID(status.subgraphDeployment) }))));
            }
            catch (error) {
                const err = errors_1.indexerError(errors_1.IndexerErrorCode.IE018, error);
                this.logger.error(`Failed to query indexing status API`, {
                    err,
                });
                throw err;
            }
        });
    }
}
exports.IndexingStatusResolver = IndexingStatusResolver;
//# sourceMappingURL=indexing-status.js.map