'use strict';

// //////////////////////////////////////////////////////////////////////////////
// DISCLAIMER
//
// Copyright 2018 ArangoDB GmbH, Cologne, Germany
//
// The Programs (which include both the software and documentation) contain
// proprietary information of ArangoDB GmbH; they are provided under a license
// agreement containing restrictions on use and disclosure and are also
// protected by copyright, patent and other intellectual and industrial
// property laws. Reverse engineering, disassembly or decompilation of the
// Programs, except to the extent required to obtain interoperability with
// other independently created software or as specified by law, is prohibited.
//
// It shall be the licensee's responsibility to take all appropriate fail-safe,
// backup, redundancy, and other measures to ensure the safe use of
// applications if the Programs are used for purposes such as nuclear,
// aviation, mass transit, medical, or other inherently dangerous applications,
// and ArangoDB GmbH disclaims liability for any damages caused by such use of
// the Programs.
//
// This software is the confidential and proprietary information of ArangoDB
// GmbH. You shall not disclose such confidential and proprietary information
// and shall use it only in accordance with the terms of the license agreement
// you entered into with ArangoDB GmbH.
//
// @author Tobias Gödderz
// //////////////////////////////////////////////////////////////////////////////

const GRAPH_PREFIX = '/_api/gharial/';

const arangosh = require('@arangodb/arangosh');
const {db} = require('internal');
const {ArangoError, errors} = require('@arangodb');
const {ERROR_BAD_PARAMETER} = errors;
const generalGraphClient = require('@arangodb/general-graph');

// NOTE: Create call does only validate client side, so no
// difference to community code here.
exports._create = function (name, edgeDefinition, orphans, options) {
  options = options || {};
  if (!options.isSmart) {
    throw new ArangoError({
      errorNum: ERROR_BAD_PARAMETER.code,
      errorMessage: `An Enterprise Graph requires the option isSmart: true`
    });
  }
  if (options.hasOwnProperty("smartGraphAttribute")) {
    throw new ArangoError({
      errorNum: ERROR_BAD_PARAMETER.code,
      errorMessage: `An Enterprise Graph disallows a smartGraphAttribute to be given`
    });
  }
  const data = {};
  if (name) {
    data.name = name;
  }
  if (edgeDefinition) {
    data.edgeDefinitions = edgeDefinition;
  }
  if (orphans) {
    data.orphanCollections = orphans;
  }
  data.options = options;


  const uri = GRAPH_PREFIX;
  const requestResult = arangosh.checkRequestResult(db._connection.POST(uri, data));
  db._flushCache();
  return generalGraphClient.__graphToClass(requestResult.graph);
};

exports._graph = generalGraphClient._graph;
exports._drop = generalGraphClient._drop;
exports._list = generalGraphClient._list;
exports._listObjects = generalGraphClient._listObjects;
exports._exists = generalGraphClient._exists;

exports._relation = generalGraphClient._relation;
exports._edgeDefinitions = generalGraphClient._edgeDefinitions;
exports._extendEdgeDefinitions = generalGraphClient._extendEdgeDefinitions;
