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 neo4j_driver_1 = require("neo4j-driver");
  const utils_1 = require("../../utils");
  const to_compose_1 = require("../to-compose");
  const create_auth_and_params_1 = __importDefault(require("../../translate/create-auth-and-params"));
  const create_auth_param_1 = __importDefault(require("../../translate/create-auth-param"));
  const constants_1 = require("../../constants");
  function cypherResolver({ field, statement }) {
      function resolve(_root, args, _context) {
          return __awaiter(this, void 0, void 0, function* () {
              const context = _context;
              const cypherStrs = [];
              let params = Object.assign(Object.assign({}, args), { auth: create_auth_param_1.default({ context }) });
              const preAuth = create_auth_and_params_1.default({ entity: field, context });
              if (preAuth[0]) {
                  params = Object.assign(Object.assign({}, params), preAuth[1]);
                  cypherStrs.push(`CALL apoc.util.validate(NOT(${preAuth[0]}), "${constants_1.AUTH_FORBIDDEN_ERROR}", [0])`);
              }
              cypherStrs.push(statement);
              const result = yield utils_1.execute({
                  cypher: cypherStrs.join("\n"),
                  params,
                  defaultAccessMode: "WRITE",
                  raw: true,
                  context,
              });
              const values = result.records.map((record) => {
                  // eslint-disable-next-line no-underscore-dangle
                  const value = record._fields[0];
                  if (["number", "string", "boolean"].includes(typeof value)) {
                      return value;
                  }
                  if (!value) {
                      return undefined;
                  }
                  if (neo4j_driver_1.isInt(value)) {
                      return Number(value);
                  }
                  if (value.identity && value.labels && value.properties) {
                      return value.properties;
                  }
                  return value;
              });
              if (!field.typeMeta.array) {
                  return values[0];
              }
              return values;
          });
      }
      return {
          type: field.typeMeta.pretty,
          resolve,
          args: to_compose_1.graphqlArgsToCompose(field.arguments),
      };
  }
  exports.default = cypherResolver;
  //# sourceMappingURL=cypher.js.map