'use strict';

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

var _browserBuf = require('../browser/browser-buf');

var _browserBuf2 = _interopRequireDefault(_browserBuf);

var _textEncoding = require('text-encoding');

var _textEncoding2 = _interopRequireDefault(_textEncoding);

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 encoder = new _textEncoding2.default.TextEncoder('utf-8');
var decoder = new _textEncoding2.default.TextDecoder('utf-8');

function encode(str) {
  return new _browserBuf2.default(encoder.encode(str).buffer);
}

function decode(buffer, length) {
  if (buffer instanceof _browserBuf2.default) {
    return decoder.decode(buffer.readView(Math.min(length, buffer.length - buffer.position)));
  } else {
    // Copy the given buffer into a regular buffer and decode that
    var tmpBuf = new _browserBuf2.default(length);
    for (var i = 0; i < length; i++) {
      tmpBuf.writeUInt8(buffer.readUInt8());
    }
    tmpBuf.reset();
    return decoder.decode(tmpBuf.readView(length));
  }
}

exports.default = {
  encode: encode,
  decode: decode
};