"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useLogic = void 0;
const react_1 = require("react");
const redirectToLink_1 = require("../../../utils/redirectToLink");
const DataGridContext_1 = require("../../DataGridContext");
const enums_1 = require("../../enums");
const constants_1 = require("../constants");
const RowContext_1 = require("../RowContext");
const hooks_1 = require("./hooks");
const utils_1 = require("./utils");
const useLogic = ({ keyId, columns, row, nestedChildren, level, variant, options, isInitialExpanded = false, expandedLevel, isSelectable, selectedRows, onSelectRow, onRowClick, isVisibleCollapseButton, registerRowElement, unregisterRowElement, style, }) => {
    const { isDrawerOpen, handleOpenDrawer, handleCloseDrawer } = (0, hooks_1.useMobileLogic)();
    const isDefaultExpanded = isInitialExpanded && level <= expandedLevel - 1 && nestedChildren?.length;
    const { checkIsOpened, toggleOpenItems } = (0, react_1.useContext)(DataGridContext_1.DataGridContext);
    const { isDisabled, disabledReason } = (0, react_1.useContext)(RowContext_1.RowContext);
    const savedElementRef = (0, react_1.useRef)(null);
    const rowRef = (element) => {
        if (element) {
            savedElementRef.current = element;
            registerRowElement?.(rowId, element);
        }
        else if (savedElementRef.current) {
            unregisterRowElement?.(rowId, savedElementRef.current);
            savedElementRef.current = null;
        }
    };
    const [isVisibleTooltip, setVisibleTooltip] = (0, react_1.useState)(false);
    const rowId = row[keyId];
    const { isDisabled: isExternalDisabled, disabledReason: externalDisabledReason, href, target, } = options || {};
    const disabled = isDisabled || isExternalDisabled;
    const isOpen = checkIsOpened(rowId);
    const handleRowClick = () => {
        if (disabled) {
            handleOpenDrawer();
            return undefined;
        }
        if (href) {
            return (0, redirectToLink_1.redirectToLink)(href, target);
        }
        onRowClick?.(row);
    };
    (0, react_1.useEffect)(() => {
        // Проверка на isOpen необходима для infinite версии с виртуализацией, когда происходит размонтировании компонента
        if (isDefaultExpanded && !isOpen) {
            toggleOpenItems(rowId);
        }
    }, []);
    const isChecked = isSelectable &&
        Boolean(selectedRows?.find((selectedRow) => selectedRow[keyId] === rowId));
    const isShowConnector = Object.is(variant, enums_1.Variant.Tree);
    const childrenColumns = (0, react_1.useMemo)(() => (0, utils_1.mergeColumnsOptions)(columns, options?.childrenColumns), [columns, options]);
    const handleToggle = (event) => {
        event.stopPropagation();
        toggleOpenItems(rowId);
    };
    const handleOpenTooltip = (event) => {
        const element = event.target;
        const isDisabledCell = element?.getAttribute(constants_1.DISABLE_ROW_ATTR) === 'true';
        if (isDisabledCell) {
            setVisibleTooltip(true);
        }
    };
    const handleCloseTooltip = () => setVisibleTooltip(false);
    const handleMouseMove = (event) => {
        if (!disabled) {
            return;
        }
        const element = event.target;
        const isDisabledCell = element.getAttribute(constants_1.DISABLE_ROW_ATTR) === 'true';
        if (!isDisabledCell) {
            setVisibleTooltip(false);
        }
    };
    const isRenderCollapseButton = Boolean(nestedChildren?.length) && isVisibleCollapseButton;
    const rowStyles = (0, react_1.useMemo)(() => ({
        '--level': level,
        ...style,
    }), [level, style]);
    const isHovered = Boolean(!disabled && (onRowClick || href));
    const disabledReasonText = externalDisabledReason || disabledReason;
    return {
        disabledReasonText,
        rowStyles,
        isOpen,
        isShowConnector,
        childrenColumns,
        rowId,
        rowRef,
        disabled,
        isRenderCollapseButton,
        handleToggle,
        isHovered,
        rowProps: {
            onClick: handleRowClick,
            onMouseMove: handleMouseMove,
        },
        drawerProps: {
            open: isDrawerOpen,
            onClose: handleCloseDrawer,
            title: 'Заблокировано',
        },
        tooltipProps: {
            open: isVisibleTooltip,
            title: disabled ? disabledReasonText : undefined,
            onOpen: handleOpenTooltip,
            onClose: handleCloseTooltip,
        },
        checkboxProps: {
            checked: isChecked,
            disabled,
            onChange: onSelectRow(row),
        },
        nestedChildrenProps: {
            isOpen,
            rowId,
        },
    };
};
exports.useLogic = useLogic;
