{"version":3,"file":"rewrite.cjs","names":[],"sources":["../../src/rewrite.ts"],"sourcesContent":["import { cleanPath, trimPath } from './path'\nimport type { LocationRewrite } from './router'\n\n/** Compose multiple rewrite pairs into a single in/out rewrite. */\nexport function composeRewrites(rewrites: Array<LocationRewrite>) {\n  return {\n    input: ({ url }) => {\n      for (const rewrite of rewrites) {\n        url = executeRewriteInput(rewrite, url)\n      }\n      return url\n    },\n    output: ({ url }) => {\n      for (let i = rewrites.length - 1; i >= 0; i--) {\n        url = executeRewriteOutput(rewrites[i], url)\n      }\n      return url\n    },\n  } satisfies LocationRewrite\n}\n\n/** Create a rewrite pair that strips/adds a basepath on input/output. */\nexport function rewriteBasepath(\n  basepath: string,\n  caseSensitive?: boolean,\n  rewrite?: LocationRewrite,\n) {\n  const trimmedBasepath = trimPath(basepath)\n  const normalizedBasepath = `/${trimmedBasepath}`\n  const checkBasepath = caseSensitive\n    ? normalizedBasepath\n    : normalizedBasepath.toLowerCase()\n  const checkBasepathWithSlash = `${checkBasepath}/`\n\n  const basepathRewrite = {\n    input: ({ url }) => {\n      const pathname = caseSensitive ? url.pathname : url.pathname.toLowerCase()\n\n      // Handle exact basepath match (e.g., /my-app -> /)\n      if (pathname === checkBasepath) {\n        url.pathname = '/'\n      } else if (pathname.startsWith(checkBasepathWithSlash)) {\n        // Handle basepath with trailing content (e.g., /my-app/users -> /users)\n        url.pathname = url.pathname.slice(normalizedBasepath.length)\n      }\n      return url\n    },\n    output: ({ url }) => {\n      // `url.pathname` always starts with \"/\", so only slashes already inside\n      // it can repeat; cleanPath keeps the joinPaths normalization.\n      url.pathname = cleanPath(`/${trimmedBasepath}${url.pathname}`)\n      return url\n    },\n  } satisfies LocationRewrite\n\n  // Strip the basepath before custom input and restore it after custom output.\n  return rewrite\n    ? ({\n        input: ({ url }) =>\n          executeRewriteInput(rewrite, basepathRewrite.input({ url })),\n        output: ({ url }) =>\n          basepathRewrite.output({ url: executeRewriteOutput(rewrite, url) }),\n      } satisfies LocationRewrite)\n    : basepathRewrite\n}\n\n/** Execute a location input rewrite if provided. */\nexport function executeRewriteInput(\n  rewrite: LocationRewrite | undefined,\n  url: URL,\n): URL {\n  const res = rewrite?.input?.({ url })\n  if (res) {\n    if (typeof res === 'string') {\n      return new URL(res)\n    } else if (res instanceof URL) {\n      return res\n    }\n  }\n  return url\n}\n\n/** Execute a location output rewrite if provided. */\nexport function executeRewriteOutput(\n  rewrite: LocationRewrite | undefined,\n  url: URL,\n): URL {\n  const res = rewrite?.output?.({ url })\n  if (res) {\n    if (typeof res === 'string') {\n      return new URL(res)\n    } else if (res instanceof URL) {\n      return res\n    }\n  }\n  return url\n}\n"],"mappings":";;;AAIA,SAAgB,gBAAgB,UAAkC;CAChE,OAAO;EACL,QAAQ,EAAE,UAAU;GAClB,KAAK,MAAM,WAAW,UACpB,MAAM,oBAAoB,SAAS,GAAG;GAExC,OAAO;EACT;EACA,SAAS,EAAE,UAAU;GACnB,KAAK,IAAI,IAAI,SAAS,SAAS,GAAG,KAAK,GAAG,KACxC,MAAM,qBAAqB,SAAS,IAAI,GAAG;GAE7C,OAAO;EACT;CACF;AACF;;AAGA,SAAgB,gBACd,UACA,eACA,SACA;CACA,MAAM,kBAAkB,aAAA,SAAS,QAAQ;CACzC,MAAM,qBAAqB,IAAI;CAC/B,MAAM,gBAAgB,gBAClB,qBACA,mBAAmB,YAAY;CACnC,MAAM,yBAAyB,GAAG,cAAc;CAEhD,MAAM,kBAAkB;EACtB,QAAQ,EAAE,UAAU;GAClB,MAAM,WAAW,gBAAgB,IAAI,WAAW,IAAI,SAAS,YAAY;GAGzE,IAAI,aAAa,eACf,IAAI,WAAW;QACV,IAAI,SAAS,WAAW,sBAAsB,GAEnD,IAAI,WAAW,IAAI,SAAS,MAAM,mBAAmB,MAAM;GAE7D,OAAO;EACT;EACA,SAAS,EAAE,UAAU;GAGnB,IAAI,WAAW,aAAA,UAAU,IAAI,kBAAkB,IAAI,UAAU;GAC7D,OAAO;EACT;CACF;CAGA,OA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