const CLIENT_ONLY_STUB_NAMESPACE = "om-client-only-stub";
const LOCAL_IMPORT_PATTERN = /^(\.{1,2}\/|@\/)/;
const CLIENT_ONLY_SUFFIX_PATTERN = /(^|[\\/])[^\\/]+\.client(\.[cm]?[jt]sx?)?$/;
function isClientOnlyModulePath(importPath) {
  if (!LOCAL_IMPORT_PATTERN.test(importPath)) return false;
  return CLIENT_ONLY_SUFFIX_PATTERN.test(importPath);
}
function encodeJsStringLiteral(value) {
  let encoded = "";
  for (let index = 0; index < value.length; index += 1) {
    const char = value[index];
    if (char === '"' || char === "\\") {
      encoded += `\\${char}`;
      continue;
    }
    const code = value.charCodeAt(index);
    if (code < 32 || code > 126) {
      encoded += `\\u${code.toString(16).padStart(4, "0")}`;
      continue;
    }
    encoded += char;
  }
  return `"${encoded}"`;
}
function renderClientOnlyModuleStub(importPath) {
  const message = `[internal] Client-only module ${importPath} is not available in the CLI runtime. It is excluded from the CLI bundle because it renders browser components.`;
  return [
    `function clientOnlyModuleUnavailable() { throw new Error(${encodeJsStringLiteral(message)}) }`,
    "export default clientOnlyModuleUnavailable"
  ].join("\n");
}
function createClientOnlyStubPlugin() {
  return {
    name: "client-only-stub",
    setup(build) {
      build.onResolve({ filter: CLIENT_ONLY_SUFFIX_PATTERN }, (args) => {
        if (args.kind !== "dynamic-import") return null;
        if (!isClientOnlyModulePath(args.path)) return null;
        return { path: args.path, namespace: CLIENT_ONLY_STUB_NAMESPACE };
      });
      build.onLoad({ filter: /.*/, namespace: CLIENT_ONLY_STUB_NAMESPACE }, (args) => ({
        contents: renderClientOnlyModuleStub(args.path),
        loader: "js"
      }));
    }
  };
}
export {
  CLIENT_ONLY_STUB_NAMESPACE,
  createClientOnlyStubPlugin,
  encodeJsStringLiteral,
  isClientOnlyModulePath,
  renderClientOnlyModuleStub
};
//# sourceMappingURL=clientOnlyModules.js.map
