'use strict';

var constant = require('./constant.cjs');

function generatePath(modules, margin = 0) {
  const ops = [];
  modules.forEach((row, y) => {
    let start = null;
    row.forEach((cell, x) => {
      if (!cell && start !== null) {
        ops.push(`M${start + margin} ${y + margin}h${x - start}v1H${start + margin}z`);
        start = null;
        return;
      }
      if (x === row.length - 1) {
        if (!cell) {
          return;
        }
        if (start === null) {
          ops.push(`M${x + margin},${y + margin} h1v1H${x + margin}z`);
        } else {
          ops.push(`M${start + margin},${y + margin} h${x + 1 - start}v1H${start + margin}z`);
        }
        return;
      }
      if (cell && start === null)
        start = x;
    });
  });
  return ops.join("");
}
function excavateModules(modules, excavation) {
  return modules.slice().map((row, y) => {
    if (y < excavation.y || y >= excavation.y + excavation.h)
      return row;
    return row.map((cell, x) => {
      if (x < excavation.x || x >= excavation.x + excavation.w)
        return cell;
      return false;
    });
  });
}
function getImageSettings(cells, size, margin, imageSettings) {
  if (imageSettings == null)
    return null;
  const numCells = cells.length + margin * 2;
  const defaultSize = Math.floor(size * constant.DEFAULT_IMG_SCALE);
  const scale = numCells / size;
  const w = (imageSettings.width || defaultSize) * scale;
  const h = (imageSettings.height || defaultSize) * scale;
  const x = imageSettings.x == null ? cells.length / 2 - w / 2 : imageSettings.x * scale;
  const y = imageSettings.y == null ? cells.length / 2 - h / 2 : imageSettings.y * scale;
  let excavation = null;
  if (imageSettings.excavate) {
    const floorX = Math.floor(x);
    const floorY = Math.floor(y);
    const ceilW = Math.ceil(w + x - floorX);
    const ceilH = Math.ceil(h + y - floorY);
    excavation = { x: floorX, y: floorY, w: ceilW, h: ceilH };
  }
  return { x, y, h, w, excavation };
}
function getMarginSize(includeMargin, marginSize) {
  if (marginSize != null)
    return Math.floor(marginSize);
  return includeMargin ? constant.SPEC_MARGIN_SIZE : constant.DEFAULT_MARGIN_SIZE;
}
const SUPPORTS_PATH2D = function() {
  try {
    new Path2D().addPath(new Path2D());
  } catch (e) {
    return false;
  }
  return true;
}();

exports.SUPPORTS_PATH2D = SUPPORTS_PATH2D;
exports.excavateModules = excavateModules;
exports.generatePath = generatePath;
exports.getImageSettings = getImageSettings;
exports.getMarginSize = getMarginSize;
//# sourceMappingURL=util.cjs.map
