/**
 * Pivot-aware coordinate adjustment.
 *
 * Background
 * ──────────
 * Both `ImageElement` and `TextElement` render with `offsetX/Y` set to
 * the bbox CENTER unconditionally — see the docblocks in those files
 * for the full rationale. The short version: Konva rotates a node
 * around its local origin (i.e. the point at `offsetX, offsetY`); with
 * offset at the bbox center, rotations pivot around the center, which
 * is what users expect from a rotate handle. Flipping (scaleX/Y = -1)
 * also reflects around the local origin, so a center-aligned offset
 * gives in-place flip as a bonus.
 *
 * Consequence for coordinate accounting: Konva reports `node.x()` /
 * `node.y()` in the post-offset frame — the node's drawn top-left
 * corner ends up at `(node.x() - offsetX, node.y() - offsetY)` on the
 * stage. To recover the un-offset "top-left of element" coords that
 * are stored on the element, we subtract `W/2, H/2` from the reported
 * x/y. This is true regardless of flip state, because the offset is
 * set unconditionally.
 *
 * This pattern appears four times — once each in
 * `ImageElement.onDragEnd` / `onTransformEnd` and
 * `TextElement.onDragEnd` / `onTransformEnd` — with `width`/`height`
 * coming from the element props for images and from the locally-
 * computed `approxW`/`approxH` for text. Same shape, two callers.
 *
 * Centralizing keeps the four sites in lockstep: any future change to
 * the offset convention only needs to update this one helper.
 *
 * Note on the `flipX` / `flipY` arguments
 * ───────────────────────────────────────
 * Previously the offset was only applied when EITHER axis was flipped
 * (the historical "flip via offset" trick); the offset was zero
 * otherwise, and this helper conditionally subtracted W/2, H/2 only in
 * the flipped case. That gave the user-facing "rotation pivots from
 * top-left for non-flipped, center for flipped" inconsistency — most
 * visible on touch devices where the rotate handle has inherent
 * sub-pixel offset on contact, swinging the bbox visibly on the
 * very first frame of rotation around the top-left pivot.
 *
 * Now the offset is always center, so the subtraction is unconditional
 * and the flipX/flipY arguments are unused. Kept in the signature so
 * call sites don't need to change — minor API stability over a
 * micro-cleanup that would be re-broken by any future per-axis offset
 * scheme.
 */

/**
 * Convert a Konva node's reported (x, y) back to the un-offset top-left
 * coordinate that's stored on the element.
 *
 * @param {Konva.Node} node     - the Konva node to read x/y from
 * @param {number}    width     - the element's bbox width
 * @param {number}    height    - the element's bbox height
 * @param {boolean}   _flipX    - element.flipX (unused; see docblock)
 * @param {boolean}   _flipY    - element.flipY (unused; see docblock)
 * @returns {{ x: number, y: number }}
 */
// eslint-disable-next-line no-unused-vars
export function unflipReportedXY(node, width, height, _flipX, _flipY) {
  return {
    x: node.x() - width / 2,
    y: node.y() - height / 2,
  };
}
