text: "use strict";
  /*
   * Copyright (c) "Neo4j"
   * Neo4j Sweden AB [http://neo4j.com]
   *
   * This file is part of Neo4j.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  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 });
  const debug_1 = __importDefault(require("debug"));
  const classes_1 = require("../classes");
  const constants_1 = require("../constants");
  const create_auth_param_1 = __importDefault(require("../translate/create-auth-param"));
  const environment_1 = __importDefault(require("../environment"));
  const debug = debug_1.default(constants_1.DEBUG_EXECUTE);
  function execute(input) {
      return __awaiter(this, void 0, void 0, function* () {
          const sessionParams = { defaultAccessMode: input.defaultAccessMode };
          const driverConfig = input.context.driverConfig;
          if (driverConfig) {
              if (driverConfig.database) {
                  sessionParams.database = driverConfig.database;
              }
              if (driverConfig.bookmarks) {
                  sessionParams.bookmarks = driverConfig.bookmarks;
              }
          }
          // @ts-ignore: Required to set connection user agent
          input.context.driver._userAgent = `${environment_1.default.NPM_PACKAGE_VERSION}/${environment_1.default.NPM_PACKAGE_NAME}`; // eslint-disable-line no-underscore-dangle
          const session = input.context.driver.session(sessionParams);
          // Its really difficult to know when users are using the `auth` param. For Simplicity it better to do the check here
          if (input.cypher.includes("$auth.") ||
              input.cypher.includes("auth: $auth") ||
              input.cypher.includes("auth:$auth")) {
              input.params.auth = create_auth_param_1.default({ context: input.context });
          }
          try {
              // input.context.neoSchema.debug(`Cypher: ${input.cypher}\nParams: ${JSON.stringify(input.params, null, 2)}`);
              debug("%s", `About to execute Cypher:\nCypher:\n${input.cypher}\nParams:\n${JSON.stringify(input.params, null, 2)}`);
              const result = yield session[`${input.defaultAccessMode.toLowerCase()}Transaction`]((tx) => tx.run(input.cypher, input.params));
              if (input.statistics) {
                  return result.summary.updateStatistics._stats; // eslint-disable-line no-underscore-dangle
              }
              if (input.raw) {
                  return result;
              }
              const records = result.records.map((r) => r.toObject());
              debug(`Execute successful, received ${records.length} records`);
              return records;
          }
          catch (error) {
              if (error.message.includes(`Caused by: java.lang.RuntimeException: ${constants_1.AUTH_FORBIDDEN_ERROR}`)) {
                  throw new classes_1.Neo4jGraphQLForbiddenError("Forbidden");
              }
              if (error.message.includes(`Caused by: java.lang.RuntimeException: ${constants_1.AUTH_UNAUTHENTICATED_ERROR}`)) {
                  throw new classes_1.Neo4jGraphQLAuthenticationError("Unauthenticated");
              }
              debug("%s", error);
              throw error;
          }
          finally {
              yield session.close();
          }
      });
  }
  exports.default = execute;
  //# sourceMappingURL=execute.js.map