// src/router/reg-exp-router/trie.ts import { Node } from "./node.js"; var Trie = class { #context = { varIndex: 0 }; #root = new Node(); #index = 0; // dynamic path -> [handler index, param assoc]; static paths are not registered paths = /* @__PURE__ */ Object.create(null); insert(path, isStatic) { if (isStatic) { this.#root.insert(path.split(""), 0, [], this.#context, true); return; } const paramAssoc = []; const groups = []; let markedPath = path; for (let i = 0; ; ) { let replaced = false; markedPath = markedPath.replace(/\{[^}]+\}/g, (m) => { const mark = `@\\${i}`; groups[i] = [mark, m]; i++; replaced = true; return mark; }); if (!replaced) { break; } } const tokens = markedPath.match(/(?::[^\/]+)|(?:\/\*$)|./g) || []; for (let i = groups.length - 1; i >= 0; i--) { const [mark] = groups[i]; for (let j = tokens.length - 1; j >= 0; j--) { if (tokens[j].indexOf(mark) !== -1) { tokens[j] = tokens[j].replace(mark, groups[i][1]); break; } } } this.#root.insert(tokens, this.#index, paramAssoc, this.#context, false); this.paths[path] = [this.#index++, paramAssoc]; } buildRegExp() { let regexp = this.#root.buildRegExpStr(); if (regexp === "") { return [/^$/, [], []]; } let captureIndex = 0; const indexReplacementMap = []; const paramReplacementMap = []; regexp = regexp.replace(/#(\d+)|@(\d+)|\.\*\$/g, (_, handlerIndex, paramIndex) => { if (handlerIndex !== void 0) { indexReplacementMap[++captureIndex] = Number(handlerIndex); return "$()"; } if (paramIndex !== void 0) { paramReplacementMap[Number(paramIndex)] = ++captureIndex; return ""; } return ""; }); return [new RegExp(`^${regexp}`), indexReplacementMap, paramReplacementMap]; } }; export { Trie };