'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');

var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _boltProtocolV = require('./bolt-protocol-v2');

var _boltProtocolV2 = _interopRequireDefault(_boltProtocolV);

var _requestMessage = require('./request-message');

var _requestMessage2 = _interopRequireDefault(_requestMessage);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
 * Copyright (c) 2002-2018 "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 BoltProtocol = function (_BoltProtocolV) {
  (0, _inherits3.default)(BoltProtocol, _BoltProtocolV);

  function BoltProtocol(connection, chunker, disableLosslessIntegers) {
    (0, _classCallCheck3.default)(this, BoltProtocol);
    return (0, _possibleConstructorReturn3.default)(this, (BoltProtocol.__proto__ || (0, _getPrototypeOf2.default)(BoltProtocol)).call(this, connection, chunker, disableLosslessIntegers));
  }

  (0, _createClass3.default)(BoltProtocol, [{
    key: 'transformMetadata',
    value: function transformMetadata(metadata) {
      if (metadata.t_first) {
        // Bolt V3 uses shorter key 't_first' to represent 'result_available_after'
        // adjust the key to be the same as in Bolt V1 so that ResultSummary can retrieve the value
        metadata.result_available_after = metadata.t_first;
        delete metadata.t_first;
      }
      if (metadata.t_last) {
        // Bolt V3 uses shorter key 't_last' to represent 'result_consumed_after'
        // adjust the key to be the same as in Bolt V1 so that ResultSummary can retrieve the value
        metadata.result_consumed_after = metadata.t_last;
        delete metadata.t_last;
      }
      return metadata;
    }
  }, {
    key: 'initialize',
    value: function initialize(userAgent, authToken, observer) {
      prepareToHandleSingleResponse(observer);
      var message = _requestMessage2.default.hello(userAgent, authToken);
      this._connection.write(message, observer, true);
    }
  }, {
    key: 'prepareToClose',
    value: function prepareToClose(observer) {
      var message = _requestMessage2.default.goodbye();
      this._connection.write(message, observer, true);
    }
  }, {
    key: 'beginTransaction',
    value: function beginTransaction(bookmark, txConfig, observer) {
      prepareToHandleSingleResponse(observer);
      var message = _requestMessage2.default.begin(bookmark, txConfig);
      this._connection.write(message, observer, true);
    }
  }, {
    key: 'commitTransaction',
    value: function commitTransaction(observer) {
      prepareToHandleSingleResponse(observer);
      var message = _requestMessage2.default.commit();
      this._connection.write(message, observer, true);
    }
  }, {
    key: 'rollbackTransaction',
    value: function rollbackTransaction(observer) {
      prepareToHandleSingleResponse(observer);
      var message = _requestMessage2.default.rollback();
      this._connection.write(message, observer, true);
    }
  }, {
    key: 'run',
    value: function run(statement, parameters, bookmark, txConfig, observer) {
      var runMessage = _requestMessage2.default.runWithMetadata(statement, parameters, bookmark, txConfig);
      var pullAllMessage = _requestMessage2.default.pullAll();

      this._connection.write(runMessage, observer, false);
      this._connection.write(pullAllMessage, observer, true);
    }
  }]);
  return BoltProtocol;
}(_boltProtocolV2.default);

exports.default = BoltProtocol;


function prepareToHandleSingleResponse(observer) {
  if (observer && typeof observer.prepareToHandleSingleResponse === 'function') {
    observer.prepareToHandleSingleResponse();
  }
}