var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/pisellFloorMapLayout/utils/alignSnap.ts
var alignSnap_exports = {};
__export(alignSnap_exports, {
  snapRectToOthers: () => snapRectToOthers
});
module.exports = __toCommonJS(alignSnap_exports);
function getSelfEdgesX(rect) {
  return [
    { edge: "left", value: rect.left },
    { edge: "centerX", value: rect.left + rect.width / 2 },
    { edge: "right", value: rect.left + rect.width }
  ];
}
function getSelfEdgesY(rect) {
  return [
    { edge: "top", value: rect.top },
    { edge: "centerY", value: rect.top + rect.height / 2 },
    { edge: "bottom", value: rect.top + rect.height }
  ];
}
function getOtherEdgesX(rect) {
  return getSelfEdgesX(rect);
}
function getOtherEdgesY(rect) {
  return getSelfEdgesY(rect);
}
function snapRectToOthers(rect, others, options) {
  const threshold = Math.max(0, options.threshold);
  if (threshold <= 0 || others.length === 0) {
    return { rect: stripId(rect), guides: [] };
  }
  const dx = findBestDelta(
    getSelfEdgesX(rect),
    others.flatMap((o) => getOtherEdgesX(o).map((e) => ({ ...e, ownerId: o.id }))),
    threshold,
    options.mode === "resize" ? options.movableEdgesX : void 0
  );
  const dy = findBestDelta(
    getSelfEdgesY(rect),
    others.flatMap((o) => getOtherEdgesY(o).map((e) => ({ ...e, ownerId: o.id }))),
    threshold,
    options.mode === "resize" ? options.movableEdgesY : void 0
  );
  const snapped = applyDelta(rect, (dx == null ? void 0 : dx.delta) ?? 0, (dy == null ? void 0 : dy.delta) ?? 0, options);
  const guides = [];
  if (dx) {
    for (const self of getSelfEdgesX(snapped)) {
      for (const o of others) {
        for (const oe of getOtherEdgesX(o)) {
          if (self.value === oe.value) {
            const top = Math.min(snapped.top, o.top);
            const bottom = Math.max(
              snapped.top + snapped.height,
              o.top + o.height
            );
            guides.push({
              orientation: "v",
              position: self.value,
              start: top,
              end: bottom,
              selfEdge: self.edge,
              otherEdge: oe.edge,
              otherId: o.id
            });
          }
        }
      }
    }
  }
  if (dy) {
    for (const self of getSelfEdgesY(snapped)) {
      for (const o of others) {
        for (const oe of getOtherEdgesY(o)) {
          if (self.value === oe.value) {
            const left = Math.min(snapped.left, o.left);
            const right = Math.max(
              snapped.left + snapped.width,
              o.left + o.width
            );
            guides.push({
              orientation: "h",
              position: self.value,
              start: left,
              end: right,
              selfEdge: self.edge,
              otherEdge: oe.edge,
              otherId: o.id
            });
          }
        }
      }
    }
  }
  return { rect: snapped, guides };
}
function findBestDelta(selfEdges, otherEdges, threshold, movableFilter) {
  let best = null;
  for (const self of selfEdges) {
    if (movableFilter) {
      if (self.edge === "centerX" || self.edge === "centerY") continue;
      if (!movableFilter.includes(self.edge)) continue;
    }
    for (const o of otherEdges) {
      const dist = o.value - self.value;
      const ad = Math.abs(dist);
      if (ad > threshold) continue;
      if (!best || ad < best.absDist) {
        best = { delta: dist, absDist: ad };
      }
    }
  }
  return best ? { delta: best.delta } : null;
}
function applyDelta(rect, dx, dy, options) {
  if (options.mode === "drag") {
    return {
      left: rect.left + dx,
      top: rect.top + dy,
      width: rect.width,
      height: rect.height
    };
  }
  let { left, top, width, height } = rect;
  const right = left + width;
  const bottom = top + height;
  const mx = options.movableEdgesX ?? [];
  const my = options.movableEdgesY ?? [];
  let newLeft = left;
  let newRight = right;
  let newTop = top;
  let newBottom = bottom;
  if (dx !== 0) {
    if (mx.includes("left")) newLeft = left + dx;
    else if (mx.includes("right")) newRight = right + dx;
  }
  if (dy !== 0) {
    if (my.includes("top")) newTop = top + dy;
    else if (my.includes("bottom")) newBottom = bottom + dy;
  }
  if (newRight - newLeft < 1) {
    newLeft = left;
    newRight = right;
  }
  if (newBottom - newTop < 1) {
    newTop = top;
    newBottom = bottom;
  }
  return {
    left: newLeft,
    top: newTop,
    width: newRight - newLeft,
    height: newBottom - newTop
  };
}
function stripId(rect) {
  return {
    left: rect.left,
    top: rect.top,
    width: rect.width,
    height: rect.height
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  snapRectToOthers
});
