'use strict';

var _istextorbinary;

function _load_istextorbinary() {
  return _istextorbinary = _interopRequireDefault(require('istextorbinary'));
}

var _replaceBufferNonRecursive;

function _load_replaceBufferNonRecursive() {
  return _replaceBufferNonRecursive = _interopRequireDefault(require('./buffer/replace-buffer-non-recursive'));
}

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

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

function converts(fileName, text, to) {
  if (Buffer.isBuffer(text)) {
    if ((_istextorbinary || _load_istextorbinary()).default.isBinarySync(fileName, text)) return text; // don't touch binary files
    newLines.forEach(function (newLine) {
      // $FlowFixMe text is Buffer here
      if (newLine !== to) text = (0, (_replaceBufferNonRecursive || _load_replaceBufferNonRecursive()).default)(text, newLine, to);
    });
    return text;
  }
  return text.toString().replace(newline, to);
}

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

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

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

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