"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.useModalEffect = exports.useModalStore = void 0;
var react_1 = __importDefault(require("react"));
var random_1 = require("../../hooks/random");
var stores_1 = require("../../utils/stores");
exports.useModalStore = (0, stores_1.create)(function (set) { return ({
    modals: [],
    addModal: function (_a) {
        var key = _a.key, isOpen = _a.isOpen, onChangeIsOpen = _a.onChangeIsOpen, content = _a.content;
        return set(function (_a) {
            var modals = _a.modals;
            return ({
                modals: __spreadArray(__spreadArray([], modals, true), [{ key: key, isOpen: isOpen, onChangeIsOpen: onChangeIsOpen, content: content }], false),
            });
        });
    },
    removeModal: function (key) {
        return set(function (_a) {
            var modals = _a.modals;
            return ({
                modals: modals.filter(function (modal) { return modal.key !== key; }),
            });
        });
    },
    updateModal: function (_a) {
        var key = _a.key, isOpen = _a.isOpen, onChangeIsOpen = _a.onChangeIsOpen, content = _a.content;
        return set(function (_a) {
            var modals = _a.modals;
            return ({
                modals: modals.map(function (modal) {
                    return modal.key === key ? { key: key, isOpen: isOpen, onChangeIsOpen: onChangeIsOpen, content: content } : modal;
                }),
            });
        });
    },
}); });
var useModalEffect = function (_a, dep) {
    var _b = _a.isOpen, isOpen = _b === void 0 ? true : _b, onChangeIsOpen = _a.onChangeIsOpen, content = _a.content;
    if (dep === void 0) { dep = []; }
    var id = (0, random_1.useDateToken)()[0];
    var _c = (0, exports.useModalStore)(), addModal = _c.addModal, removeModal = _c.removeModal, updateModal = _c.updateModal;
    react_1.default.useEffect(function () {
        addModal({ key: id, isOpen: isOpen, onChangeIsOpen: onChangeIsOpen, content: content });
        return function () { return removeModal(id); };
    }, []);
    react_1.default.useEffect(function () {
        updateModal({ key: id, isOpen: isOpen, onChangeIsOpen: onChangeIsOpen, content: content });
    }, __spreadArray([isOpen], dep, true));
};
exports.useModalEffect = useModalEffect;
//# sourceMappingURL=modal.js.map