"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = (module)=>{
        var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    DragHandle: ()=>DragHandle
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const styles_namespaceObject = require("@mui/material/styles");
const apollo_core_namespaceObject = require("@uipath/apollo-core");
var apollo_core_default = /*#__PURE__*/ __webpack_require__.n(apollo_core_namespaceObject);
const external_react_namespaceObject = require("react");
var external_react_default = /*#__PURE__*/ __webpack_require__.n(external_react_namespaceObject);
const chat_service_provider_cjs_namespaceObject = require("../../providers/chat-service.provider.cjs");
const chat_state_provider_cjs_namespaceObject = require("../../providers/chat-state-provider.cjs");
const chat_width_provider_cjs_namespaceObject = require("../../providers/chat-width-provider.cjs");
const index_cjs_namespaceObject = require("../../service/index.cjs");
const DragHandleContainer = (0, styles_namespaceObject.styled)('div')(()=>({
        position: 'absolute',
        left: 0,
        top: 0,
        width: apollo_core_default().Border.BorderThickS,
        height: '100%',
        background: 'var(--color-border-de-emp)',
        cursor: 'ew-resize',
        transition: 'background 0.2s ease',
        userSelect: 'none',
        zIndex: 2,
        '&::before': {
            content: '""',
            position: 'absolute',
            top: 0,
            left: -7,
            width: 15,
            height: '100%',
            cursor: 'ew-resize',
            userSelect: 'none',
            zIndex: 2
        },
        '&:hover, &:focus, &:active': {
            background: 'var(--color-focus-indicator)',
            outline: 'none'
        }
    }));
const Border = (0, styles_namespaceObject.styled)('div')(()=>({
        position: 'absolute',
        left: 0,
        top: 0,
        width: apollo_core_default().Border.BorderThickS,
        height: '100%',
        background: 'var(--color-border-de-emp)'
    }));
function DragHandleComponent() {
    const isDraggingRef = external_react_default().useRef(false);
    const startXRef = external_react_default().useRef(0);
    const startWidthRef = external_react_default().useRef(0);
    const { width, setWidth, setShouldAnimate } = (0, chat_width_provider_cjs_namespaceObject.useChatWidth)();
    const chatInternalService = (0, chat_service_provider_cjs_namespaceObject.useChatService)().__internalService__;
    const { disabledFeatures } = (0, chat_state_provider_cjs_namespaceObject.useChatState)();
    const widthRef = external_react_default().useRef(width);
    widthRef.current = width;
    const handleMouseMove = external_react_default().useCallback((e)=>{
        if (!isDraggingRef.current) return;
        const delta = startXRef.current - e.clientX;
        const newWidth = Math.min(Math.max(startWidthRef.current + delta, index_cjs_namespaceObject.CHAT_WIDTH_SIDE_BY_SIDE_MIN), index_cjs_namespaceObject.CHAT_WIDTH_SIDE_BY_SIDE_MAX);
        widthRef.current = newWidth;
        chatInternalService.publish(index_cjs_namespaceObject.AutopilotChatInternalEvent.ChatResize, newWidth);
        setWidth(newWidth);
    }, [
        setWidth,
        chatInternalService
    ]);
    const handleMouseUp = external_react_default().useCallback(()=>{
        isDraggingRef.current = false;
        document.removeEventListener('mousemove', handleMouseMove);
        document.removeEventListener('mouseup', handleMouseUp);
        index_cjs_namespaceObject.StorageService.Instance.set(index_cjs_namespaceObject.CHAT_WIDTH_KEY, widthRef.current.toString());
    }, [
        handleMouseMove
    ]);
    const handleMouseDown = external_react_default().useCallback((e)=>{
        e.preventDefault();
        isDraggingRef.current = true;
        startXRef.current = e.clientX;
        startWidthRef.current = widthRef.current;
        document.addEventListener('mousemove', handleMouseMove);
        document.addEventListener('mouseup', handleMouseUp);
    }, [
        handleMouseMove,
        handleMouseUp
    ]);
    const handleKeyDown = external_react_default().useCallback((e)=>{
        const step = 10;
        if (widthRef.current < index_cjs_namespaceObject.CHAT_WIDTH_SIDE_BY_SIDE_MIN || widthRef.current > index_cjs_namespaceObject.CHAT_WIDTH_SIDE_BY_SIDE_MAX) return;
        if ('ArrowLeft' === e.key) {
            e.preventDefault();
            const newWidth = Math.min(widthRef.current + step, index_cjs_namespaceObject.CHAT_WIDTH_SIDE_BY_SIDE_MAX);
            widthRef.current = newWidth;
            setWidth(newWidth);
            chatInternalService.publish(index_cjs_namespaceObject.AutopilotChatInternalEvent.ChatResize, newWidth);
            setShouldAnimate(true);
        } else if ('ArrowRight' === e.key) {
            e.preventDefault();
            const newWidth = Math.max(widthRef.current - step, index_cjs_namespaceObject.CHAT_WIDTH_SIDE_BY_SIDE_MIN);
            widthRef.current = newWidth;
            setWidth(newWidth);
            chatInternalService.publish(index_cjs_namespaceObject.AutopilotChatInternalEvent.ChatResize, newWidth);
            setShouldAnimate(true);
        }
    }, [
        setWidth,
        setShouldAnimate,
        chatInternalService
    ]);
    const handleKeyUp = external_react_default().useCallback(()=>{
        setShouldAnimate(false);
        index_cjs_namespaceObject.StorageService.Instance.set(index_cjs_namespaceObject.CHAT_WIDTH_KEY, widthRef.current.toString());
    }, [
        setShouldAnimate
    ]);
    external_react_default().useEffect(()=>()=>{
            document.removeEventListener('mousemove', handleMouseMove);
            document.removeEventListener('mouseup', handleMouseUp);
        }, [
        handleMouseMove,
        handleMouseUp
    ]);
    if (disabledFeatures.resize) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Border, {});
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(DragHandleContainer, {
        onMouseDown: handleMouseDown,
        onKeyDown: handleKeyDown,
        onKeyUp: handleKeyUp,
        tabIndex: 0,
        role: "separator",
        "aria-valuenow": width,
        "aria-valuemin": index_cjs_namespaceObject.CHAT_WIDTH_SIDE_BY_SIDE_MIN,
        "aria-valuemax": index_cjs_namespaceObject.CHAT_WIDTH_SIDE_BY_SIDE_MAX
    });
}
const DragHandle = /*#__PURE__*/ external_react_default().memo(DragHandleComponent);
exports.DragHandle = __webpack_exports__.DragHandle;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "DragHandle"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
