"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PlacementArray = exports.ColorTypeArray = void 0;
const react_1 = __importStar(require("react"));
const Styled = __importStar(require("./index.styled"));
const Colors_1 = require("../../../core/Colors");
const Icon_1 = require("../../../Icon");
exports.ColorTypeArray = ['white', 'pink', 'blue', 'gray', 'lime'];
exports.PlacementArray = [
    'top-left',
    'top-center',
    'top-right',
    'bottom-left',
    'bottom-center',
    'bottom-right',
    'left-center',
    'right-center',
];
const arrowLengthPx = 5.8;
const distancePx = 2;
const createArrow = ({ backgroundColor }) => {
    if (backgroundColor === Colors_1.basicColors.white) {
        // fyi. borderColor 컬러가 상이해서 다른 svg를 사용합니다.
        return `<svg width="13" height="8" viewBox="0 0 13 8" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M4.67395 2.01734L5.08584e-07 8L1.31297 8L5.46343 2.67569C5.86314 2.16294 6.63816 2.16193 7.03919 2.67364L11.2136 8L12.5 8L7.82605 2.01734C7.02531 0.992394 5.4747 0.992392 4.67395 2.01734Z" fill="${Colors_1.basicColors.gray100}" />
        <path d="M11.201 8L1.30078 8L5.45142 2.66295C5.85118 2.14892 6.62777 2.14801 7.02874 2.6611L11.201 8Z" fill="${Colors_1.basicColors.white}"/>
    </svg>`;
    }
    else {
        return `
    <svg id="hello" width="13" height="7" viewBox="0 0 13 7" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M4.67395 0.768711L0 6.75137L1.30078 6.75137L11.201 6.75137H12.5L7.82605 0.768713C7.02531 -0.256236 5.4747 -0.256238 4.67395 0.768711Z" fill="${backgroundColor}"/>
    </svg>
`;
    }
};
const TooltipBase = ({ placement, colorType, content, children, className, alwaysShow = false, hasCloseButton = false, zIndex, popperOptions, isChildrenFullWidth = false, }) => {
    const [visible, setVisible] = (0, react_1.useState)(false);
    const placementAssertedType = (0, react_1.useMemo)(() => {
        switch (placement) {
            case 'top-left':
                return 'bottom-start';
            case 'top-center':
                return 'bottom';
            case 'top-right':
                return 'bottom-end';
            case 'bottom-left':
                return 'top-start';
            case 'bottom-center':
                return 'top';
            case 'bottom-right':
                return 'top-end';
            case 'left-center':
                return 'right';
            case 'right-center':
                return 'left';
            default:
                return 'top';
        }
    }, [placement]);
    const colorStyle = (0, react_1.useMemo)(() => {
        switch (colorType) {
            case 'gray':
                return {
                    backgroundColor: Colors_1.basicColors.gray700,
                    borderColor: Colors_1.basicColors.gray700,
                    contentColor: Colors_1.basicColors.gray25,
                };
            case 'white':
                return {
                    backgroundColor: Colors_1.basicColors.white,
                    borderColor: Colors_1.basicColors.gray100,
                    contentColor: Colors_1.basicColors.gray400,
                };
            default:
                return {
                    backgroundColor: Colors_1.basicColors[`${colorType}400`],
                    borderColor: Colors_1.basicColors[`${colorType}400`],
                    contentColor: Colors_1.basicColors.gray25,
                };
        }
    }, [colorType]);
    const propsWithoutPlacement = {
        visible,
        arrow: createArrow({ backgroundColor: colorStyle.backgroundColor }),
    };
    const openTooltip = () => {
        setVisible(true);
    };
    const closeTooltip = () => {
        setVisible(false);
    };
    const convertedContent = (react_1.default.createElement(Styled.TippyContentAndCloseButtonWrapper, { onClick: closeTooltip },
        react_1.default.createElement("div", null, content),
        hasCloseButton && (react_1.default.createElement(Styled.CloseButtonWrapper, null,
            react_1.default.createElement(Icon_1.CancelSIcon, { fillColor: Colors_1.basicColors.white })))));
    const handleMouseEnter = () => {
        if (visible && alwaysShow) {
            return;
        }
        openTooltip();
    };
    const handleMouseLeave = () => {
        if (visible && alwaysShow) {
            return;
        }
        closeTooltip();
    };
    (0, react_1.useEffect)(() => {
        if (alwaysShow) {
            // 초기 렌더링에 툴팁 표시
            setVisible(true);
        }
    }, [alwaysShow]);
    return (react_1.default.createElement(Styled.TippyWrapper, Object.assign({}, colorStyle, propsWithoutPlacement, { placement: placementAssertedType, className: className, content: convertedContent, offset: [0, arrowLengthPx + distancePx], interactive: true, zIndex: zIndex, popperOptions: popperOptions }),
        react_1.default.createElement(Styled.Children, { isChildrenFullWidth: isChildrenFullWidth, onMouseEnter: handleMouseEnter, onMouseLeave: handleMouseLeave }, children)));
};
exports.default = TooltipBase;
