Files
discord-decomp/_bundle/getStateFromPath.js
T
2026-11-01 12:12:55 +02:00

370 lines
11 KiB
JavaScript

// Metro module 1524 (f1525).
import validatePathConfig from "./validatePathConfig";
import Module1525 from "./Module1525";
import findFocusedRoute from "./findFocusedRoute";
import * as _validateArrayFormatSeparator from "./validateArrayFormatSeparator";
let matchAgainstConfigs, createNormalizedConfigs, createConfigItem, findInitialRoute, createStateObject, createNestedStateObject, parseQueryParams, $mk69364 = function() {
return (a0) => a0.startsWith(":") ? `(([^/]+\\/)${a0.endsWith("?") ? "?" : ""})` : `${a0 !== "*" ? Module1525(a0) : ".*"}\\/`;
};
const joinPaths = function joinPaths(...args) {
const r4 = [];
return r4.concat.apply(r4, [...args.map((arg) => arg.split("/"))]).filter(Boolean).join("/");
};
matchAgainstConfigs = (remainingPath, a1) => {
let routes, c13085_3;
const _loop = function _loop() {
if (c13085_3.regex) {
let c24453_1;
const r13 = remainingPath.match(c13085_3.regex);
let c24453_0 = r13;
if (r13) {
const pattern = c13085_3.pattern;
c24453_1 = pattern == null ? void 0 : pattern.split("/").filter((item) => item.startsWith(":")).reduce((acc, item, index) => {
const r4 = Object;
const r2 = {};
r2[item] = c24453_0[2 * (index + 1)].replace(/\//, "");
return r4.assign(acc, r2);
}, {});
routes = c13085_3.routeNames.map((routeName) => {
let c28905_1;
const r10 = a1.find((item) => item.screen === routeName);
c28905_1 = r10;
const r2 = r10 == null;
let params = void 0;
if (!r2) {
const path = r10.path;
params = path == null ? void 0 : path.split("/").filter((item) => item.startsWith(":")).reduce((acc, item) => {
const r6 = c24453_1[item];
if (r6) {
const r13 = item.replace(/^:/, "").replace(/\?$/, "");
const parse = c28905_1.parse;
acc[r13] = (parse == null ? void 0 : parse[r13]) ? c28905_1.parse[r13](r6) : r6;
}
return acc;
}, {});
}
return params && Object.keys(params).length ? {
name: routeName,
params
} : {
name: routeName
};
});
remainingPath = remainingPath.replace(r13[1], "");
return 1;
}
return void 0;
}
return 0;
};
for (const item of a1) {
c13085_3 = item;
const r24 = _loop();
if (r24 === 0) {
continue;
}
if (r24 !== 1) {
continue;
}
break;
}
return {
routes,
remainingPath
};
};
createNormalizedConfigs = function createNormalizedConfigs(a0, a1, r5 = []) {
let c13086_6;
let c13086_0 = r5;
const r12 = arguments[3];
let c13086_1 = r12;
const parentScreens = arguments[4];
let c13086_2 = parentScreens;
const r6 = arguments[5];
let c13086_3 = r6;
c13086_6 = void 0;
const r3 = [];
let c13086_4 = r3;
r5.push(a0);
parentScreens.push(a0);
const path2 = a1[a0];
let c13086_5 = path2;
if (typeof path2 === "string") {
const pattern2 = (r6 ? joinPaths(r6, path2) : path2).split("/").filter(Boolean).join("/");
r3.push({
screen: a0,
regex: pattern2 ? new RegExp(`^(${pattern2.split("/").map($mk69364()).join("")})`) : void 0,
pattern: pattern2,
path: path2,
routeNames: [...r5],
parse: void 0
});
} else if (typeof path2 === "object") {
if (typeof path2.path === "string") {
if (path2.exact && path2.path === void 0) {
throw new Error("A 'path' needs to be specified when specifying 'exact: true'. If you don't want this screen in the URL, specify it as empty string, e.g. `path: ''`.");
}
const r17 = path2.exact !== true ? joinPaths(r6 || "", path2.path || "") : path2.path || "";
c13086_6 = r17;
const { path, parse } = path2;
const pattern = r17.split("/").filter(Boolean).join("/");
r3.push({
screen: a0,
regex: pattern ? new RegExp(`^(${pattern.split("/").map($mk69364()).join("")})`) : void 0,
pattern,
path,
routeNames: [...r5],
parse
});
}
if (path2.screens) {
if (path2.initialRouteName) {
r12.push({
initialRouteName: path2.initialRouteName,
parentScreens
});
}
Object.keys(path2.screens).forEach((item) => {
const screens2 = c13086_5.screens;
const r6 = [...c13086_2];
let r5 = c13086_6;
if (r5 == null) {
r5 = c13086_3;
}
const normalizedConfigs = createNormalizedConfigs(item, screens2, c13086_0, c13086_1, r6, r5);
c13086_4.push.apply(c13086_4, [...normalizedConfigs]);
});
}
}
r5.pop();
return r3;
};
createConfigItem = (a0, a1, a2, a3, a4) => {};
findInitialRoute = (name, a1, a2) => {
let r40;
for (const item of a2) {
if (a1.length !== item.parentScreens.length) {
continue;
}
let r31 = true;
let r32 = 0;
if (0 < a1.length) {
do {
const r13 = r32;
if (a1[r13].localeCompare(item.parentScreens[r13]) !== 0) {
r31 = false;
break;
}
r40 = r32 + 1;
r32 = r40;
} while (r40 < a1.length);
}
if (!r31) {
continue;
}
return name !== item.initialRouteName ? item.initialRouteName : void 0;
}
};
createStateObject = (a0, a1, a2) => {};
createNestedStateObject = (a0, routes2, a2, a3) => {
const r12 = routes2.shift();
const r7 = [];
const name = findInitialRoute(r12.name, r7, a2);
r7.push(r12.name);
const r36 = routes2.length === 0 ? name ? {
index: 1,
routes: [{
name
}, r12]
} : {
routes: [r12]
} : name ? {
index: 1,
routes: [{
name
}, {
...r12,
state: {
routes: []
}
}]
} : {
routes: [{
...r12,
state: {
routes: []
}
}]
};
if (routes2.length > 0) {
let r51 = routes2.shift();
let r52 = r36;
while (r51) {
const name2 = findInitialRoute(r51.name, r7, a2);
let index = r52.index;
const r56 = r52;
const r57 = r51;
if (!index) {
index = r56.routes.length - 1;
}
r56.routes[index].state = routes2.length === 0 ? name2 ? {
index: 1,
routes: [{
name: name2
}, r57]
} : {
routes: [r57]
} : name2 ? {
index: 1,
routes: [{
name: name2
}, {
...r57,
state: {
routes: []
}
}]
} : {
routes: [{
...r57,
state: {
routes: []
}
}]
};
const r84 = routes2.length > 0 ? r56.routes[index].state : r56;
r7.push(r57.name);
r51 = routes2.shift();
r52 = r84;
}
}
const r97 = findFocusedRoute(r36);
r97.path = a0;
const r99 = a3 ? ((a0, a1) => {
for (const item of a1) {
if (a0 !== item.routeNames[item.routeNames.length - 1]) {
continue;
}
return item.parse;
}
})(r97.name, a3) : void 0;
let c13087_0 = r99;
const r113 = _validateArrayFormatSeparator.parse(a0.split("?")[1]);
let c13087_1 = r113;
if (r99) {
Object.keys(r113).forEach((item) => {
const hasOwnProperty = Object.hasOwnProperty;
if (hasOwnProperty.call(c13087_0, item) && typeof c13087_1[item] === "string") {
c13087_1[item] = c13087_0[item](c13087_1[item]);
}
});
}
const r126 = Object.keys(r113).length ? r113 : void 0;
if (r126) {
r97.params = {
...r97.params,
...r126
};
}
return r36;
};
parseQueryParams = (a0, a1) => {};
export default function getStateFromPath(a0, a1) {
if (a1) {
validatePathConfig(a1);
}
let c13088_2;
const r8 = [];
let c13088_0 = r8;
if (a1?.initialRouteName) {
r8.push({
initialRouteName: a1.initialRouteName,
parentScreens: []
});
}
const screens = a1?.screens;
const r40 = a0.replace(/\/+/g, "/").replace(/^\//, "").replace(/\?.*$/, "");
const r43 = r40.endsWith("/") ? r40 : `${r40}/`;
if (screens === void 0) {
const r100 = r43.split("/").filter(Boolean).map((item) => ({
name: decodeURIComponent(item)
}));
return r100.length ? createNestedStateObject(a0, r100, r8) : void 0;
}
const r47 = [];
const r61 = r47.concat.apply(r47, [...Object.keys(screens).map((item) => createNormalizedConfigs(item, screens, [], c13088_0, []))]).sort((a, b) => {
if (a.pattern === b.pattern) {
return b.routeNames.join(">").localeCompare(a.routeNames.join(">"));
}
if (a.pattern.startsWith(b.pattern)) {
return -1;
}
if (b.pattern.startsWith(a.pattern)) {
return 1;
}
const r26 = a.pattern.split("/");
const r29 = b.pattern.split("/");
const r32 = Math.max(r26.length, r29.length);
let r1 = 0;
if (0 < r32) {
do {
const r35 = r26[r1];
const r0 = r1;
if (r35 == null) {
return 1;
}
if (r29[r0] == null) {
return -1;
}
const r38 = r26[r0] === "*" || r26[r0].startsWith(":");
const r41 = r29[r0] === "*" || r29[r0].startsWith(":");
if (!(r38 && r41)) {
if (r38) {
return 1;
}
if (r41) {
return -1;
}
}
r1 = r0 + 1;
} while (r1 < Math.max(r26.length, r29.length));
}
return r29.length - r26.length;
});
c13088_2 = r61;
r61.reduce((acc, a1) => {
if (acc[a1.pattern]) {
const routeNames = acc[a1.pattern].routeNames;
const routeNames2 = a1.routeNames;
if (!(routeNames.length > routeNames2.length ? routeNames2.every((item, index) => routeNames[index] === item) : routeNames.every((routeName, index) => routeNames2[index] === routeName))) {
throw new Error(`Found conflicting screens with the same pattern. The pattern '${a1.pattern}' resolves to both '${routeNames.join(" > ")}' and '${routeNames2.join(" > ")}'. Patterns must be unique and cannot resolve to more than one screen.`);
}
}
const r39 = Object;
const r41 = {};
r41[a1.pattern] = a1;
return r39.assign(acc, r41);
}, {});
if (r43 === "/") {
const r81 = r61.find((a0) => a0.path === "" && a0.routeNames.every((c28906_0) => !c13088_2.find((item) => item.screen === c28906_0)?.path));
return r81 ? createNestedStateObject(a0, r81.routeNames.map((routeName) => ({
name: routeName
})), r8, r61) : void 0;
}
const r70 = matchAgainstConfigs(r43, r61.map((item) => ({
...item,
regex: item.regex ? new RegExp(item.regex.source + "$") : void 0
})));
const routes2 = r70.routes;
r70.remainingPath;
let r73 = void 0;
let r74 = void 0;
if (routes2 !== void 0) {
r73 = createNestedStateObject(a0, routes2, r8, r61);
r74 = r73;
}
if (r73 != null && r74 != null) {
return r74;
}
};