"use strict";
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useTooltipEffect = exports.useTooltipStore = void 0;
var react_1 = __importDefault(require("react"));
var random_1 = require("../../hooks/random");
var stores_1 = require("../../utils/stores");
exports.useTooltipStore = (0, stores_1.create)(function (set) { return ({
    tooltips: [],
    addTooltip: function (_a) {
        var key = _a.key, content = _a.content;
        return set(function (_a) {
            var tooltips = _a.tooltips;
            return ({
                tooltips: __spreadArray(__spreadArray([], tooltips, true), [{ key: key, content: content }], false),
            });
        });
    },
    removeTooltip: function (key) {
        return set(function (_a) {
            var tooltips = _a.tooltips;
            return ({
                tooltips: tooltips.filter(function (tooltip) { return tooltip.key !== key; }),
            });
        });
    },
    updateTooltip: function (_a) {
        var key = _a.key, content = _a.content;
        return set(function (_a) {
            var tooltips = _a.tooltips;
            return ({
                tooltips: tooltips.map(function (tooltip) {
                    return tooltip.key === key ? { key: key, content: content } : tooltip;
                }),
            });
        });
    },
}); });
var useTooltipEffect = function (content, isDisplay, dep) {
    if (isDisplay === void 0) { isDisplay = true; }
    if (dep === void 0) { dep = []; }
    var id = (0, random_1.useDateToken)()[0];
    var _a = (0, exports.useTooltipStore)(), addTooltip = _a.addTooltip, removeTooltip = _a.removeTooltip, updateTooltip = _a.updateTooltip;
    react_1.default.useEffect(function () {
        if (!isDisplay)
            return;
        addTooltip({ key: id, content: content });
        return function () { return removeTooltip(id); };
    }, [isDisplay]);
    react_1.default.useEffect(function () {
        updateTooltip({ key: id, content: content });
    }, __spreadArray([isDisplay], dep, true));
};
exports.useTooltipEffect = useTooltipEffect;
//# sourceMappingURL=tooltip.js.map