import { GRID_CHECKBOX_SELECTION_COL_DEF } from '../../../../colDef';
import { buildWarning } from '../../../../utils/warning';
function sanitizeCellValue(value, delimiterCharacter) {
  if (typeof value === 'string') {
    var formattedValue = value.replace(/"/g, '""');

    // Make sure value containing delimiter or line break won't be split into multiple rows
    if ([delimiterCharacter, '\n', '\r'].some(function (delimiter) {
      return formattedValue.includes(delimiter);
    })) {
      return "\"".concat(formattedValue, "\"");
    }
    return formattedValue;
  }
  return value;
}
export var serializeCellValue = function serializeCellValue(cellParams, options) {
  var delimiterCharacter = options.delimiterCharacter,
    ignoreValueFormatter = options.ignoreValueFormatter;
  var value;
  if (ignoreValueFormatter) {
    var _cellParams$value2;
    var columnType = cellParams.colDef.type;
    if (columnType === 'number') {
      value = String(cellParams.value);
    } else if (columnType === 'date' || columnType === 'dateTime') {
      var _cellParams$value;
      value = (_cellParams$value = cellParams.value) == null ? void 0 : _cellParams$value.toISOString();
    } else if (typeof ((_cellParams$value2 = cellParams.value) == null ? void 0 : _cellParams$value2.toString) === 'function') {
      value = cellParams.value.toString();
    } else {
      value = cellParams.value;
    }
  } else {
    value = cellParams.formattedValue;
  }
  return sanitizeCellValue(value, delimiterCharacter);
};
var objectFormattedValueWarning = buildWarning(['MUI: When the value of a field is an object or a `renderCell` is provided, the CSV export might not display the value correctly.', 'You can provide a `valueFormatter` with a string representation to be used.']);
var serializeRow = function serializeRow(_ref) {
  var id = _ref.id,
    columns = _ref.columns,
    getCellParams = _ref.getCellParams,
    delimiterCharacter = _ref.delimiterCharacter,
    ignoreValueFormatter = _ref.ignoreValueFormatter;
  return columns.map(function (column) {
    var cellParams = getCellParams(id, column.field);
    if (process.env.NODE_ENV !== 'production') {
      if (String(cellParams.formattedValue) === '[object Object]') {
        objectFormattedValueWarning();
      }
    }
    return serializeCellValue(cellParams, {
      delimiterCharacter: delimiterCharacter,
      ignoreValueFormatter: ignoreValueFormatter
    });
  });
};
export function buildCSV(options) {
  var columns = options.columns,
    rowIds = options.rowIds,
    getCellParams = options.getCellParams,
    delimiterCharacter = options.delimiterCharacter,
    includeHeaders = options.includeHeaders,
    ignoreValueFormatter = options.ignoreValueFormatter;
  var CSVBody = rowIds.reduce(function (acc, id) {
    return "".concat(acc).concat(serializeRow({
      id: id,
      columns: columns,
      getCellParams: getCellParams,
      delimiterCharacter: delimiterCharacter,
      ignoreValueFormatter: ignoreValueFormatter
    }).join(delimiterCharacter), "\r\n");
  }, '').trim();
  if (!includeHeaders) {
    return CSVBody;
  }
  var CSVHead = "".concat(columns.filter(function (column) {
    return column.field !== GRID_CHECKBOX_SELECTION_COL_DEF.field;
  }).map(function (column) {
    return sanitizeCellValue(column.headerName || column.field, delimiterCharacter);
  }).join(delimiterCharacter), "\r\n");
  return "".concat(CSVHead).concat(CSVBody).trim();
}