"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (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;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
    for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
        to[j] = from[i];
    return to;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useIsModalRoute = exports.useRouteIsModal = exports.Route = exports.RoutersManager = void 0;
var react_1 = __importStar(require("react"));
var react_router_dom_1 = require("react-router-dom");
var path_to_regexp_1 = require("path-to-regexp");
var RoutersManagerContext = react_1.createContext({});
var RouterModalGroupContext = react_1.createContext({});
function RoutersManager(_a) {
    var children = _a.children;
    var location = react_router_dom_1.useLocation();
    var _b = react_1.useState(), previousLocation = _b[0], setPreviousLocation = _b[1];
    var _c = react_1.useState(false), animationModal = _c[0], setAnimationModal = _c[1];
    var _d = react_1.useState([]), routes = _d[0], setRoutes = _d[1];
    var routeHistory = react_1.useRef([]);
    var currentRoute = react_1.useRef(undefined);
    react_1.useLayoutEffect(function () {
        var _a, _b, _c, _d;
        var route = routes.find(function (i) {
            return !!path_to_regexp_1.pathToRegexp(i.path).exec(location.pathname);
        });
        var oldRouteModalInfo = routes.find(function (i) {
            var _a;
            return !!path_to_regexp_1.pathToRegexp(i.path).exec((_a = currentRoute.current) === null || _a === void 0 ? void 0 : _a.pathname);
        });
        var currentRoutePos = routeHistory.current.findIndex(function (i) { return (i === null || i === void 0 ? void 0 : i.key) === currentRoute.current.key; });
        var newRoutePos = routeHistory.current.findIndex(function (i) { return (i === null || i === void 0 ? void 0 : i.key) === location.key; });
        var savePreviousLocation = route && !route.modal;
        savePreviousLocation = savePreviousLocation || !((_a = currentRoute.current) === null || _a === void 0 ? void 0 : _a.key);
        savePreviousLocation =
            savePreviousLocation ||
                (currentRoutePos > newRoutePos &&
                    newRoutePos !== -1 &&
                    !(oldRouteModalInfo === null || oldRouteModalInfo === void 0 ? void 0 : oldRouteModalInfo.modal) &&
                    (previousLocation === null || previousLocation === void 0 ? void 0 : previousLocation.pathname) != (location === null || location === void 0 ? void 0 : location.pathname));
        savePreviousLocation =
            savePreviousLocation ||
                (currentRoutePos > newRoutePos &&
                    newRoutePos !== -1 &&
                    (oldRouteModalInfo === null || oldRouteModalInfo === void 0 ? void 0 : oldRouteModalInfo.modal) &&
                    (previousLocation === null || previousLocation === void 0 ? void 0 : previousLocation.pathname) == ((_b = currentRoute.current) === null || _b === void 0 ? void 0 : _b.pathname));
        savePreviousLocation =
            savePreviousLocation ||
                (currentRoutePos < newRoutePos &&
                    (previousLocation === null || previousLocation === void 0 ? void 0 : previousLocation.pathname) == ((_c = currentRoute.current) === null || _c === void 0 ? void 0 : _c.pathname) &&
                    (oldRouteModalInfo === null || oldRouteModalInfo === void 0 ? void 0 : oldRouteModalInfo.modal));
        var modalAnimation = (previousLocation === null || previousLocation === void 0 ? void 0 : previousLocation.pathname) == ((_d = currentRoute.current) === null || _d === void 0 ? void 0 : _d.pathname);
        setAnimationModal(modalAnimation);
        if (savePreviousLocation) {
            setPreviousLocation(location);
        }
        if (!routeHistory.current.find(function (route) { return route.key == location.key; })) {
            routeHistory.current.push(location);
        }
        currentRoute.current = location;
    }, [location, routes]);
    function addRoute(data) {
        setRoutes(function (i) {
            var a = __spreadArray([], i);
            a.push(data);
            return a;
        });
    }
    return (react_1.default.createElement(RoutersManagerContext.Provider, { value: {
            addRoute: addRoute,
            routes: routes,
            previousLocation: previousLocation,
            animation: animationModal,
            isModal: (previousLocation === null || previousLocation === void 0 ? void 0 : previousLocation.key) !== (location === null || location === void 0 ? void 0 : location.key),
        } },
        react_1.default.createElement(react_router_dom_1.Switch, { location: previousLocation }, children),
        react_1.default.createElement(RouterModalGroupContext.Provider, { value: { modal: true } }, children)));
}
exports.RoutersManager = RoutersManager;
function Route(props) {
    var _a, _b;
    var ctx = react_1.useContext(RouterModalGroupContext);
    var ctxManager = react_1.useContext(RoutersManagerContext);
    react_1.useEffect(function () {
        if (ctx.modal) {
            ctxManager.addRoute({
                path: props.path,
                modal: !!props.modal,
            });
        }
    }, []);
    if ((ctx.modal && !props.modal) ||
        (ctx.modal && !!path_to_regexp_1.pathToRegexp(props.path).exec((_a = ctxManager.previousLocation) === null || _a === void 0 ? void 0 : _a.pathname)) ||
        ((_b = ctxManager.previousLocation) === null || _b === void 0 ? void 0 : _b.pathname) == undefined)
        return null;
    return react_1.default.createElement(react_router_dom_1.Route, __assign({}, props));
}
exports.Route = Route;
function useRouteIsModal() {
    var ctx = react_1.useContext(RouterModalGroupContext);
    return ctx.modal;
}
exports.useRouteIsModal = useRouteIsModal;
function useIsModalRoute() {
    var ctxManager = react_1.useContext(RoutersManagerContext);
    return {
        showModal: ctxManager.isModal,
        animation: ctxManager.animation,
    };
}
exports.useIsModalRoute = useIsModalRoute;
//# sourceMappingURL=RoutersManager.js.map