/* @oracle/oraclejet-preact: undefined */
'use strict';

var jsxRuntime = require('preact/jsx-runtime');
var hooks = require('preact/hooks');
var PRIVATE_VisOverview_themes_VisOverview_css = require('./PRIVATE_VisOverview/themes/VisOverview.css.js');
var eventsUtils = require('./eventsUtils-1abb6028.js');
require('./accUtils-dcffb3de.js');
var DragH = require('./DragH-03bc7b1f.js');
var clientUtils = require('./clientUtils-c5a6872f.js');

/**
 * @license
 * Copyright (c) %FIRST_YEAR% %CURRENT_YEAR%, Oracle and/or its affiliates.
 * The Universal Permissive License (UPL), Version 1.0
 * as shown at https://oss.oracle.com/licenses/upl/
 * @ignore
 */
const OVERVIEW_MIN_SIZE = 20;
function useOverviewEvents({ x, width, windowX, windowWidth, rootRef, windowHeight, windowY, onWindowDimsChange }) {
    const pointerDownPointRef = hooks.useRef();
    const [lastPoint, setLastPoint] = hooks.useState();
    const onPointerDown = hooks.useCallback((event) => {
        pointerDownPointRef.current = { x: event.offsetX, y: event.offsetY };
        event.target.setPointerCapture(event.pointerId);
        setLastPoint({ x: event.offsetX, y: event.offsetY });
    }, [pointerDownPointRef]);
    const onPointerUp = hooks.useCallback((event) => {
        const { offsetX: clickX, offsetY: clickY } = event;
        const isPointerUpFar = Math.abs(pointerDownPointRef.current.x - clickX) > OVERVIEW_MIN_SIZE / 4;
        pointerDownPointRef.current = undefined;
        setLastPoint(undefined);
        if ((clickX <= windowX + windowWidth &&
            clickX >= windowX &&
            clickY <= windowY + windowHeight &&
            clickY >= windowY) ||
            isPointerUpFar) {
            return;
        }
        const newX = Math.min(x + width - windowWidth, Math.max(clickX - windowWidth / 2, x));
        onWindowDimsChange?.({ deltaX: newX - windowX, deltaWidth: 0 });
    }, [pointerDownPointRef, windowHeight, windowX, windowY, windowWidth, width, x, onWindowDimsChange]);
    const onPointerMove = hooks.useCallback((event) => {
        const targetType = eventsUtils.getElementData(rootRef, event.target)?.['ojObject'];
        if (!pointerDownPointRef.current ||
            event.target == null ||
            targetType == null ||
            lastPoint == null) {
            return;
        }
        let deltaX = 0;
        let deltaWidth = 0;
        if (targetType === 'window') {
            const delta = event.offsetX - lastPoint.x;
            deltaX = Math.min(x + width - windowWidth, Math.max(x, windowX + delta)) - windowX;
        }
        else if (targetType === 'leftHandle' &&
            event.offsetX <= windowX + windowWidth - OVERVIEW_MIN_SIZE / 2) {
            deltaX = Math.max(x, windowX + event.offsetX - lastPoint.x) - windowX;
            deltaWidth = -1 * deltaX;
        }
        else if (targetType === 'rightHandle' && event.offsetX >= windowX + OVERVIEW_MIN_SIZE / 2) {
            deltaWidth = Math.min(event.offsetX - lastPoint.x, x + width - windowX);
        }
        setLastPoint({ x: event.offsetX, y: event.offsetY });
        onWindowDimsChange?.({ deltaX, deltaWidth });
        event.stopPropagation();
    }, [pointerDownPointRef, rootRef, x, lastPoint, width, windowX, windowWidth, onWindowDimsChange]);
    return {
        onPointerDown,
        onPointerUp,
        onPointerMove
    };
}

const OVERVIEW_HANDLE_SIZE = 16;
const OVERVIEW_HANDLE_ACTIVATION_SIZE = clientUtils.isTouch() ? 30 : 10;
function OverviewSlidingWindow({ x, width, height, y, windowX, windowHeight, windowWidth, windowY, hasHandles, onWindowDimsChange }) {
    const rootRef = hooks.useRef(null);
    const overviewProps = useOverviewEvents({
        x,
        y,
        width,
        height,
        windowHeight,
        windowWidth,
        windowX,
        windowY,
        rootRef,
        onWindowDimsChange
    });
    const handleIconY = windowY + 0.5 * (windowHeight - OVERVIEW_HANDLE_SIZE);
    return (jsxRuntime.jsxs("g", { ref: rootRef, ...(hasHandles ? overviewProps : {}), children: [jsxRuntime.jsx("rect", { x: x, y: y, width: width, height: height, fill: "transparent" }), jsxRuntime.jsx("rect", { "data-oj-object": "window", cursor: "move", x: windowX, y: windowY, height: windowHeight - 1, width: windowWidth - 1, class: PRIVATE_VisOverview_themes_VisOverview_css.styles.overviewWindow }), hasHandles && (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsx("g", { transform: `translate(${windowX} ${handleIconY})`, children: jsxRuntime.jsx(DragH.SvgDragH, {}) }), jsxRuntime.jsx("g", { transform: `translate(${windowX + windowWidth - OVERVIEW_HANDLE_SIZE} ${handleIconY})`, children: jsxRuntime.jsx(DragH.SvgDragH, {}) }), jsxRuntime.jsx("rect", { x: windowX, y: windowY, height: windowHeight, width: OVERVIEW_HANDLE_ACTIVATION_SIZE, strokeWidth: "1", "data-oj-object": "leftHandle", fill: "transparent", stroke: "transparent", cursor: "col-resize" }), jsxRuntime.jsx("rect", { x: windowX + windowWidth - OVERVIEW_HANDLE_ACTIVATION_SIZE, y: windowY, strokeWidth: "1", height: windowHeight, "data-oj-object": "rightHandle", width: OVERVIEW_HANDLE_ACTIVATION_SIZE, stroke: "transparent", fill: "transparent", cursor: "col-resize" })] }))] }));
}

exports.OverviewSlidingWindow = OverviewSlidingWindow;
//# sourceMappingURL=OverviewSlidingWindow-7719ad86.js.map
