/* a drag, as the events a finger sends: press, hold past the action's own
   320ms, walk down (or up) to just past the target row's middle, let go.
   Both rows are measured BEFORE the press, because the action takes its
   own snapshot at the lift and that is the geometry it will compare
   against. Returns false when the list is too short to drag — a real
   library is not obliged to hold three rows — so a thin walk says so in
   its expectation rather than dying here. cdp.evaluate awaits promises. */
window.__drag = function (sel, from, to, holdMs) {
    var rows = document.querySelectorAll(sel);
    var a = rows[from], b = rows[to];
    if (rows.length < 2 || !a || !b) return false;
    var ra = a.getBoundingClientRect(), rb = b.getBoundingClientRect();
    var x = Math.round(ra.left + Math.min(40, ra.width / 2));
    var y0 = Math.round(ra.top + ra.height / 2);
    var y1 = Math.round(rb.top + rb.height / 2 + (to > from ? 4 : -4));
    function ev(type, cy) {
        a.dispatchEvent(new PointerEvent(type, { bubbles: true, cancelable: true, composed: true, pointerId: 7, pointerType: 'touch', isPrimary: true, button: 0, buttons: type === 'pointerup' ? 0 : 1, clientX: x, clientY: cy }));
    }
    var nap = function (ms) { return new Promise(function (r) { setTimeout(r, ms); }); };
    return (async function () {
        ev('pointerdown', y0);
        await nap(holdMs || 420);
        for (var i = 1; i <= 8; i++) { ev('pointermove', Math.round(y0 + (y1 - y0) * (i / 8))); await nap(24); }
        await nap(60);
        ev('pointerup', y1);
        return true;
    })();
};
/* the other half of the same gesture: pressed and released without moving,
   which is still the row's sheet */
window.__hold = function (sel, i, holdMs) {
    var a = document.querySelectorAll(sel)[i || 0];
    if (!a) throw new Error('no row to hold for ' + sel);
    var r = a.getBoundingClientRect();
    var x = Math.round(r.left + Math.min(40, r.width / 2)), y = Math.round(r.top + r.height / 2);
    function ev(type) {
        a.dispatchEvent(new PointerEvent(type, { bubbles: true, cancelable: true, composed: true, pointerId: 8, pointerType: 'touch', isPrimary: true, button: 0, buttons: type === 'pointerup' ? 0 : 1, clientX: x, clientY: y }));
    }
    return new Promise(function (res) { ev('pointerdown'); setTimeout(function () { ev('pointerup'); res(true); }, holdMs || 420); });
};
