{
  "version": 3,
  "sources": ["../../src/document/models/CanvasTree/nodes/utils/routesNode.ts"],
  "sourcesContent": ["import type { BaseTreeStore } from \"document/base-engine/BaseTreeStore.ts\"\nimport { HeaderRouteNode, type HeaderRouteNodeInterface } from \"document/models/CanvasTree/nodes/HeaderRouteNode.ts\"\nimport type { NodeID } from \"document/models/CanvasTree/nodes/NodeID.ts\"\nimport { RedirectRouteNode } from \"document/models/CanvasTree/nodes/RedirectRouteNode.ts\"\nimport type { RouteNode } from \"document/models/CanvasTree/nodes/RouteNode.ts\"\nimport { ROUTES_NODE_ID, RoutesNode } from \"document/models/CanvasTree/nodes/RoutesNode.ts\"\nimport type { LinkToWebPage } from \"document/models/CanvasTree/traits/utils/Link.ts\"\nimport { assert } from \"library/utils/assert.ts\"\nimport { ProxyRouteNode, type ProxyRouteNodeInterface } from \"../ProxyRouteNode.ts\"\nimport { RewriteRouteNode, type RewriteRouteNodeInterface } from \"../RewriteRouteNode.ts\"\nimport { isHeaderRouteNode, isProxyRouteNode, isRedirectRouteNode, isRewriteRouteNode } from \"./nodeCheck.ts\"\n\nexport function addRedirect(\n\ttreeStore: BaseTreeStore,\n\tfrom: string,\n\tto: string | LinkToWebPage,\n\tensureLimits?: () => void,\n\texpandToAllLocales?: boolean,\n) {\n\tensureLimits?.()\n\tconst routesNode = RoutesNode.ensure(treeStore.tree)\n\tconst redirectNode = new RedirectRouteNode({\n\t\tpath: from,\n\t\tto,\n\t\texpandToAllLocales,\n\t})\n\ttreeStore.tree.insertNode(redirectNode, routesNode.id)\n\treturn redirectNode\n}\n\nexport function addRewrite(treeStore: BaseTreeStore, attributes: RewriteRouteNodeInterface, ensureLimits?: () => void) {\n\tensureLimits?.()\n\tconst routesNode = RoutesNode.ensure(treeStore.tree)\n\tconst rewriteRouteNode = new RewriteRouteNode(attributes)\n\ttreeStore.tree.insertNode(rewriteRouteNode, routesNode.id)\n\treturn rewriteRouteNode\n}\n\nexport function addProxy(treeStore: BaseTreeStore, attributes: ProxyRouteNodeInterface, ensureLimits?: () => void) {\n\tensureLimits?.()\n\tconst routesNode = RoutesNode.ensure(treeStore.tree)\n\tconst proxyRouteNode = new ProxyRouteNode(attributes)\n\ttreeStore.tree.insertNode(proxyRouteNode, routesNode.id)\n\treturn proxyRouteNode\n}\n\nexport function updateRedirect(\n\tredirectNode: RedirectRouteNode,\n\tattributes: Partial<RedirectRouteNode>,\n\tensureLimits?: () => void,\n) {\n\tensureLimits?.()\n\treturn redirectNode.set(attributes)\n}\n\nexport function updateRewrite(\n\trewriteRouteNode: RewriteRouteNode,\n\tattributes: Partial<RewriteRouteNodeInterface>,\n\tensureLimits?: () => void,\n) {\n\tensureLimits?.()\n\treturn rewriteRouteNode.set(attributes)\n}\n\nexport function updateProxy(\n\tproxyRouteNode: ProxyRouteNode,\n\tattributes: Partial<ProxyRouteNodeInterface>,\n\tensureLimits?: () => void,\n) {\n\tensureLimits?.()\n\treturn proxyRouteNode.set(attributes)\n}\n\nexport function addHeader(treeStore: BaseTreeStore, attributes: HeaderRouteNodeInterface, ensureLimits?: () => void) {\n\tensureLimits?.()\n\tconst routesNode = RoutesNode.ensure(treeStore.tree)\n\tconst headerRouteNode = new HeaderRouteNode({\n\t\tpath: attributes.path,\n\t\theaders: [\n\t\t\t{\n\t\t\t\tid: crypto.randomUUID(),\n\t\t\t\tkey: attributes.key,\n\t\t\t\tvalue: attributes.value,\n\t\t\t},\n\t\t],\n\t})\n\ttreeStore.tree.insertNode(headerRouteNode, routesNode.id)\n\treturn headerRouteNode\n}\n\nexport function updateHeader(\n\theaderRouteNode: HeaderRouteNode,\n\tattributes: Partial<HeaderRouteNodeInterface>,\n\tensureLimits?: () => void,\n) {\n\tensureLimits?.()\n\treturn headerRouteNode.set(attributes)\n}\n\n/**\n * Calculates the index in the routes node children for a route node at a specific index.\n *\n * @param routesNode - The routes node instance.\n * @param routeNodes - The array of route nodes.\n * @param targetIndex - The target index in the route nodes array.\n * @returns The calculated index in the routes node children.\n */\nfunction calculateRouteNodeIndex(routesNode: RoutesNode, routeNodes: readonly RouteNode[], targetIndex: number) {\n\tconst currentRouteNodeAtTargetIndex = routeNodes[targetIndex]\n\tassert(currentRouteNodeAtTargetIndex, `Expected a route node to exist at index`)\n\tconst routesNewIndex = routesNode.children.findIndex(node => node.id === currentRouteNodeAtTargetIndex.id)\n\tassert(routesNewIndex !== -1, `Expected route node to exist in RoutesNode.children`)\n\treturn routesNewIndex\n}\n\nexport function moveRedirect(treeStore: BaseTreeStore, newIndex: number, oldIndex: number, ensureLimits?: () => void) {\n\tensureLimits?.()\n\n\tconst routesNode = RoutesNode.ensure(treeStore.tree).loaded\n\tassert(routesNode, \"Expected the routes node to be loaded\")\n\tconst redirects = routesNode.getRedirects()\n\tconst routesNewIndex = calculateRouteNodeIndex(routesNode, redirects, newIndex)\n\n\tconst nodeToMove = redirects[oldIndex]\n\tassert(nodeToMove, `Expected a node at index \"${oldIndex}\" to exist`)\n\ttreeStore.tree.moveNode(nodeToMove, routesNode.id, routesNewIndex)\n}\n\nexport function moveRewrite(treeStore: BaseTreeStore, newIndex: number, oldIndex: number, ensureLimits?: () => void) {\n\tensureLimits?.()\n\n\tconst routesNode = RoutesNode.ensure(treeStore.tree).loaded\n\tassert(routesNode, \"Expected the routes node to be loaded\")\n\tconst rewrites = routesNode.getRewriteRoutes()\n\tconst routesNewIndex = calculateRouteNodeIndex(routesNode, rewrites, newIndex)\n\n\tconst nodeToMove = rewrites[oldIndex]\n\tassert(nodeToMove, `Expected a node at index \"${oldIndex}\" to exist`)\n\ttreeStore.tree.moveNode(nodeToMove, routesNode.id, routesNewIndex)\n}\n\nexport function moveHeader(treeStore: BaseTreeStore, newIndex: number, oldIndex: number, ensureLimits?: () => void) {\n\tensureLimits?.()\n\n\tconst routesNode = RoutesNode.ensure(treeStore.tree).loaded\n\tassert(routesNode, \"Expected the routes node to be loaded\")\n\tconst headers = routesNode.getHeaderRoutes()\n\tconst routesNewIndex = calculateRouteNodeIndex(routesNode, headers, newIndex)\n\n\tconst nodeToMove = headers[oldIndex]\n\tassert(nodeToMove, `Expected a node at index \"${oldIndex}\" to exist`)\n\ttreeStore.tree.moveNode(nodeToMove, routesNode.id, routesNewIndex)\n}\n\n/**\n * Set the order of redirects in the tree.\n *\n * @param treeStore - Engine tree store to perform the operation on.\n * @param redirectIds - The IDs of the redirect nodes to move.\n * @param ensureLimits - The IDs of the redirect nodes to move.\n */\nexport function moveRedirects(treeStore: BaseTreeStore, redirectIds: NodeID[], ensureLimits?: () => void) {\n\tensureLimits?.()\n\n\tconst routesNode = RoutesNode.ensure(treeStore.tree).loaded\n\tassert(routesNode, \"Expected the routes node to be loaded\")\n\tconst redirects = routesNode.getRedirects()\n\n\tfor (const [newIndex, redirectId] of redirectIds.entries()) {\n\t\tconst oldIndex = redirects.findIndex(redirect => redirect.id === redirectId)\n\t\tassert(oldIndex !== -1, `Expected redirect with id \u201C${redirectId}\u201D to exist`)\n\n\t\tconst redirectNode = treeStore.tree.get<RedirectRouteNode>(redirectId)\n\t\tassert(redirectNode)\n\n\t\tconst routesNewIndex = calculateRouteNodeIndex(routesNode, redirects, newIndex)\n\t\ttreeStore.tree.moveNode(redirectNode, ROUTES_NODE_ID, routesNewIndex)\n\t}\n}\n\nexport function deleteRedirects(treeStore: BaseTreeStore, redirectNodeIds: NodeID[]) {\n\tfor (const redirectNodeId of redirectNodeIds) {\n\t\tconst redirectNode = treeStore.tree.get<RedirectRouteNode>(redirectNodeId)\n\t\t// If the node is not a RedirectRouteNode, we don't need to delete it,\n\t\t// so we just skip it.\n\t\tif (!isRedirectRouteNode(redirectNode)) continue\n\t\ttreeStore.tree.removeNode(redirectNode)\n\t}\n}\n\nexport function deleteRewrites(treeStore: BaseTreeStore, rewriteNodeIds: NodeID[]) {\n\tfor (const rewriteNodeId of rewriteNodeIds) {\n\t\tconst rewriteNode = treeStore.tree.get<RewriteRouteNode>(rewriteNodeId)\n\t\t// If the node is not a RewriteRouteNode, we don't need to delete it,\n\t\t// so we just skip it.\n\t\tif (!isRewriteRouteNode(rewriteNode)) continue\n\t\ttreeStore.tree.removeNode(rewriteNode)\n\t}\n}\n\nexport function deleteCustomHeaders(treeStore: BaseTreeStore, headerNodeIds: NodeID[]) {\n\tfor (const headerNodeId of headerNodeIds) {\n\t\tconst headerNode = treeStore.tree.get<HeaderRouteNode>(headerNodeId)\n\t\t// If the node is not a RewriteRouteNode, we don't need to delete it,\n\t\t// so we just skip it.\n\t\tif (!isHeaderRouteNode(headerNode)) continue\n\t\ttreeStore.tree.removeNode(headerNode)\n\t}\n}\n\nexport function deleteProxies(treeStore: BaseTreeStore, proxyNodeIds: NodeID[]) {\n\tfor (const proxyNodeId of proxyNodeIds) {\n\t\tconst proxyNode = treeStore.tree.get<ProxyRouteNode>(proxyNodeId)\n\t\tif (!isProxyRouteNode(proxyNode)) continue\n\t\ttreeStore.tree.removeNode(proxyNode)\n\t}\n}\n"],
  "mappings": ";;;;;;;;;;;;;;;;;;;AAYO,SAAS,YACf,WACA,MACA,IACA,cACA,oBACC;AACD,iBAAe;AACf,QAAM,aAAa,WAAW,OAAO,UAAU,IAAI;AACnD,QAAM,eAAe,IAAI,kBAAkB;AAAA,IAC1C,MAAM;AAAA,IACN;AAAA,IACA;AAAA,EACD,CAAC;AACD,YAAU,KAAK,WAAW,cAAc,WAAW,EAAE;AACrD,SAAO;AACR;AAEO,SAAS,WAAW,WAA0B,YAAuC,cAA2B;AACtH,iBAAe;AACf,QAAM,aAAa,WAAW,OAAO,UAAU,IAAI;AACnD,QAAM,mBAAmB,IAAI,iBAAiB,UAAU;AACxD,YAAU,KAAK,WAAW,kBAAkB,WAAW,EAAE;AACzD,SAAO;AACR;AAEO,SAAS,SAAS,WAA0B,YAAqC,cAA2B;AAClH,iBAAe;AACf,QAAM,aAAa,WAAW,OAAO,UAAU,IAAI;AACnD,QAAM,iBAAiB,IAAI,eAAe,UAAU;AACpD,YAAU,KAAK,WAAW,gBAAgB,WAAW,EAAE;AACvD,SAAO;AACR;AAEO,SAAS,eAC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