"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getMsRawTemplateColumnOrRowStyles = exports.getAreasInfo = void 0;
var _prop2 = _interopRequireDefault(require("ramda/src/prop"));
var _indexOf2 = _interopRequireDefault(require("ramda/src/indexOf"));
var _uniq2 = _interopRequireDefault(require("ramda/src/uniq"));
var _flatten2 = _interopRequireDefault(require("ramda/src/flatten"));
var _max2 = _interopRequireDefault(require("ramda/src/max"));
var _min2 = _interopRequireDefault(require("ramda/src/min"));
var _reduce2 = _interopRequireDefault(require("ramda/src/reduce"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var minimal = (0, _reduce2.default)(_min2.default, Infinity);
var maximal = (0, _reduce2.default)(_max2.default, -Infinity);
var spacesNotInsideParentheses = /\s+(?=[^)]*([(]|$))/g;
var getAreasInfo = exports.getAreasInfo = function getAreasInfo(areas, isGapPresented) {
  var size = {
    col: areas[0].length,
    row: areas.length
  };
  var flatAreas = (0, _flatten2.default)(areas);
  var areaNames = (0, _uniq2.default)(flatAreas);
  var areaInfos = Object.values(flatAreas.reduce(function (acc, areaName, idx) {
    if (!acc[areaName]) acc[areaName] = {
      coordinates: [],
      id: (0, _indexOf2.default)(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((0, _prop2.default)('row'));
    var cols = coordinates === null || coordinates === void 0 ? void 0 : coordinates.map((0, _prop2.default)('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;
  }, {});
};
var getMsRawTemplateColumnOrRowStyles = exports.getMsRawTemplateColumnOrRowStyles = function getMsRawTemplateColumnOrRowStyles(raw, gutter) {
  return raw.replace(spacesNotInsideParentheses, " ".concat(gutter, " "));
};