import _prop from "ramda/src/prop";
import _indexOf from "ramda/src/indexOf";
import _uniq from "ramda/src/uniq";
import _flatten from "ramda/src/flatten";
import _max from "ramda/src/max";
import _min from "ramda/src/min";
import _reduce from "ramda/src/reduce";
var minimal = _reduce(_min, Infinity);
var maximal = _reduce(_max, -Infinity);
var spacesNotInsideParentheses = /\s+(?=[^)]*([(]|$))/g;
export var getAreasInfo = function getAreasInfo(areas, isGapPresented) {
  var size = {
    col: areas[0].length,
    row: areas.length
  };
  var flatAreas = _flatten(areas);
  var areaNames = _uniq(flatAreas);
  var areaInfos = Object.values(flatAreas.reduce(function (acc, areaName, idx) {
    if (!acc[areaName]) acc[areaName] = {
      coordinates: [],
      id: _indexOf(areaName, areaNames) + 1,
      name: areaName
    };
    var row = Math.trunc(idx / size.col);
    var col = Math.trunc(idx % size.col);
    acc[areaName].coordinates.push({
      row: row,
      col: col
    });
    return acc;
  }, {}));
  return areaInfos.reduce(function (acc, area) {
    var coordinates = area.coordinates,
      name = area.name,
      id = area.id;
    var rows = coordinates === null || coordinates === void 0 ? void 0 : coordinates.map(_prop('row'));
    var cols = coordinates === null || coordinates === void 0 ? void 0 : coordinates.map(_prop('col'));
    var rowStart = minimal(rows);
    var rowSpan = maximal(rows) - rowStart;
    if (isGapPresented.row) {
      rowSpan *= 2;
      rowStart *= 2;
    }
    var colStart = minimal(cols);
    var colSpan = maximal(cols) - colStart;
    if (isGapPresented.col) {
      colSpan *= 2;
      colStart *= 2;
    }
    acc[name] = {
      id: id,
      rowStart: rowStart + 1,
      rowSpan: rowSpan + 1,
      colStart: colStart + 1,
      colSpan: colSpan + 1
    };
    return acc;
  }, {});
};
export var getMsRawTemplateColumnOrRowStyles = function getMsRawTemplateColumnOrRowStyles(raw, gutter) {
  return raw.replace(spacesNotInsideParentheses, " ".concat(gutter, " "));
};