import { jsxs, jsx } from 'react/jsx-runtime';
import { useState, useRef, useEffect } from 'react';
import Icon from '../Icon/Icon.js';
import Typography from '../Typography/Typography.js';
import { truncateText, isTextTruncated } from '../../utils/truncateText/truncateText.js';
import classNames from '../../_virtual/index.js';
import Tooltip from '../Tooltip/Tooltip.js';
import useClickOutside from '../../hooks/useClickOutside.js';
import Search from '../Search/Search.js';
import { checkEmpty } from '../../utils/checkEmpty/checkEmpty.js';

const AllProjectsDropdown = ({ onClick = () => { }, onMenuClick = () => { }, options, selectedOption = {
    label: 'All Projects',
    value: 'All Projects',
    iconName: 'all_projects',
}, selected = false, placeholder, disabled = false, }) => {
    const [showOptions, setShowOptions] = useState(false);
    const [showClicked, setShowClicked] = useState(false);
    const [selectedOptions, setSelectedOptions] = useState(selectedOption);
    const optionsRef = useRef(null);
    const [optionsList, setOptionsList] = useState(options);
    // ⇩⇩⇩ Updated: use actual return type of setTimeout (works in Node & DOM) ⇩⇩⇩
    const [closeTimeout, setCloseTimeout] = useState(null);
    // ⇧⇧⇧ Updated: use actual return type of setTimeout (works in Node & DOM) ⇧⇧⇧
    const containerRef = useRef(null);
    const [searchValue, setSearchValue] = useState('');
    const [isSecondClickFromOtherTab, setIsSecondClickFromOtherTab] = useState(selected);
    useEffect(() => {
        if (optionsList !== options) {
            setOptionsList(options);
        }
    }, [options]);
    useEffect(() => {
        if (!selected) {
            setIsSecondClickFromOtherTab(false);
        }
    }, [selected]);
    useEffect(() => {
        if (selectedOption) {
            setSelectedOptions(selectedOption);
        }
    }, [selectedOption]);
    const closeOptions = () => {
        if (showOptions) {
            setShowOptions(false);
        }
    };
    const handleMouseEnter = () => {
        if (!disabled) {
            setShowOptions(true);
        }
        if (!disabled && !selected) {
            setShowOptions(true);
        }
    };
    const handleClick = () => {
        if (!disabled) {
            if (showOptions) {
                setShowOptions(false);
            }
            if (!isSecondClickFromOtherTab) {
                setIsSecondClickFromOtherTab(true);
            }
            else {
                setShowClicked(!showClicked);
            }
        }
        setSearchValue('');
        setOptionsList(options);
    };
    const handleMouseLeave = () => {
        if (!disabled) {
            const timeout = setTimeout(() => {
                closeOptions();
            }, 250);
            setCloseTimeout(timeout);
        }
    };
    const dropDownMouseEnter = () => {
        if (closeTimeout !== null) {
            clearTimeout(closeTimeout);
            setCloseTimeout(null);
        }
    };
    const handleSelectOption = (option) => {
        if (!disabled) {
            setSelectedOptions(option);
            closeOptions();
            onClick(option);
            setSearchValue('');
            setOptionsList(options);
        }
        setShowClicked(false);
        setShowOptions(false);
    };
    const handleSearch = (query) => {
        if (!disabled) {
            setSearchValue(query);
            if (!checkEmpty(query)) {
                const staticItem = options[0] ?? { label: '', value: '', iconName: '' };
                const filterData = options
                    .slice(1)
                    .filter((option) => option.label.toLowerCase().includes(query.toLowerCase()));
                setOptionsList([staticItem, ...filterData]);
            }
            else {
                setSearchValue('');
                setOptionsList(options);
            }
        }
    };
    useClickOutside(optionsRef, () => setShowClicked(false), [containerRef]);
    return (jsxs("div", { className: classNames('ff-all-project', {
            'ff-all-project--disabled': disabled,
        }), ref: containerRef, onClick: handleClick, onMouseEnter: handleMouseEnter, onMouseLeave: handleMouseLeave, children: [jsx("div", { className: classNames('ff-all-project-dropdown', {
                    ['ff-all-project-dropdown--selected']: selected,
                    ['ff-all-project-dropdown--disabled']: disabled,
                }), children: jsxs("div", { className: classNames('ff-all-project-container'), children: [jsx(Typography, { as: 'div', lineHeight: '18px', fontSize: 12, fontWeight: selected ? 'semi-bold' : 'regular', className: classNames('projects-label'), onClick: onMenuClick, children: selectedOptions.label.length <= 12
                                ? selectedOptions.label
                                : truncateText(selectedOptions?.label, 12) }), jsxs("div", { className: classNames('label-icon'), children: [jsx("div", { className: "icon-click-layer" }), jsx(Icon, { name: showOptions || showClicked
                                        ? 'arrows_top_icon'
                                        : 'arrows_down_icon', color: disabled
                                        ? 'var(--disabled-icon-color)'
                                        : 'var(--primary-icon-color)', height: 8, width: 8, hoverEffect: false })] })] }) }), (showOptions || showClicked) && !disabled && (jsxs("div", { className: classNames('ff-projects-dropdown'), ref: optionsRef, onMouseEnter: dropDownMouseEnter, children: [jsx("div", { className: "ff-dropdown-search-container", onClick: (e) => e.stopPropagation(), children: jsx(Search, { onSearch: handleSearch, value: searchValue, isExpand: true, onClose: () => { }, onExpand: () => { }, showClose: false, placeholder: placeholder, disabled: disabled, showToaster: false }) }), jsx("div", { className: classNames('option-card'), children: optionsList.map((option, index) => index === 0 && (jsxs("div", { onClick: (e) => {
                                e.stopPropagation();
                                handleSelectOption(option);
                            }, className: classNames('ff-projects-options', {
                                ['ff-selected-option']: selectedOption.label === option.label,
                                ['ff-option-disabled']: disabled,
                            }), children: [jsx("div", { className: classNames('ff-projects-icons'), children: jsx(Icon, { name: option.iconName, height: 12, width: 12 }) }), jsx(Typography, { as: 'div', lineHeight: '30px', className: "ff-projects-label", children: option.label }, index)] }, index))) }), jsx("div", { children: optionsList.length === 1 && (jsx("div", { className: "ff-no-data-found", children: "-No Projects-" })) }), jsx("div", { className: classNames('option-card', `${optionsList.length > 4 ? 'scroll' : ''}`), children: optionsList.map((option, index) => index > 0 && (jsxs("div", { onClick: (e) => {
                                e.stopPropagation();
                                handleSelectOption(option);
                            }, className: classNames('ff-projects-options', {
                                ['ff-selected-option']: selectedOption.label === option.label,
                                ['ff-option-disabled']: disabled,
                            }), children: [jsx("div", { className: classNames('ff-projects-icons'), children: jsx(Icon, { name: option.iconName, height: 12, width: 12 }) }), jsx(Typography, { as: 'div', lineHeight: '30px', className: "ff-projects-label", children: jsx(Tooltip, { title: isTextTruncated(option.label, 165, 'pixel')
                                            ? option.label
                                            : '', children: truncateText(option.label, 165, 'pixel') }) }, index)] }, index))) })] }))] }));
};

export { AllProjectsDropdown as default };
//# sourceMappingURL=AllProjectsDropdown.js.map
