"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

function _isbinaryfile() {
  const data = require("isbinaryfile");

  _isbinaryfile = function () {
    return data;
  };

  return data;
}

function _replaceBufferNonRecursive() {
  const data = _interopRequireDefault(require("./buffer/replace-buffer-non-recursive"));

  _replaceBufferNonRecursive = function () {
    return data;
  };

  return data;
}

const isWindows = typeof process !== 'undefined' && process.platform === 'win32';
const lineBreak = isWindows ? '\r\n' : '\n';
const newLines = ['\r\n', '\r', '\n'];
const newline = /\r\n|\r|\n/g;

function converts(text, to) {
  if (Buffer.isBuffer(text)) {
    if ((0, _isbinaryfile().isBinaryFileSync)(text)) return text; // don't touch binary files

    newLines.forEach(newLine => {
      // $FlowFixMe text is Buffer here
      if (newLine !== to) text = (0, _replaceBufferNonRecursive().default)(text, newLine, to);
    });
    return text;
  }

  return text.toString().replace(newline, to);
}

exports.lf = function (text) {
  return converts(text, '\n');
};

exports.auto = function (text) {
  return converts(text, lineBreak);
};

exports.cr = function (text) {
  return converts(text, '\r');
};

exports.crlf = function (text) {
  return converts(text, '\r\n');
};