var __defProp = Object.defineProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, {
      get: all[name],
      enumerable: true,
      configurable: true,
      set: (newValue) => all[name] = () => newValue
    });
};
var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);

// src/plugin.ts
import { readFileSync, existsSync } from "fs";
import { dirname, basename, join, resolve } from "path";
function findModulePath(hypenPath, patterns) {
  const dir = dirname(hypenPath);
  const baseName = basename(hypenPath, ".hypen");
  const parentDir = dirname(dir);
  const folderName = basename(dir);
  for (const pattern of patterns) {
    let candidatePath = null;
    switch (pattern) {
      case "sibling":
        candidatePath = join(dir, `${baseName}.ts`);
        break;
      case "component":
        if (baseName === "component") {
          candidatePath = join(dir, "component.ts");
        }
        break;
      case "index":
        if (baseName === "index") {
          candidatePath = join(dir, "index.ts");
        }
        break;
    }
    if (candidatePath && existsSync(candidatePath)) {
      return candidatePath;
    }
  }
  return null;
}
function getComponentName(hypenPath) {
  const baseName = basename(hypenPath, ".hypen");
  if (baseName === "component" || baseName === "index") {
    return basename(dirname(hypenPath));
  }
  return baseName;
}
function parseImports(text) {
  const imports = [];
  const importRegex = /import\s+(?:\{([^}]+)\}|(\w+))\s+from\s+["']([^"']+)["']/g;
  let match;
  while ((match = importRegex.exec(text)) !== null) {
    const [, namedImports, defaultImport, source] = match;
    if (!source)
      continue;
    let names;
    if (namedImports) {
      names = namedImports.split(",").map((n) => n.trim()).filter((n) => n.length > 0);
    } else if (defaultImport) {
      names = [defaultImport];
    } else {
      continue;
    }
    imports.push({ names, source });
  }
  return imports;
}
function removeImports(text) {
  return text.replace(/import\s+(?:\{[^}]+\}|\w+)\s+from\s+["'][^"']+["']\s*/g, "");
}
function hypenPlugin(options = {}) {
  const { debug = false, patterns = ["sibling", "component", "index"] } = options;
  const log = debug ? (...args) => console.log("[hypen-plugin]", ...args) : () => {};
  return {
    name: "hypen-loader",
    async setup(build) {
      build.onLoad({ filter: /\.hypen$/ }, async (args) => {
        const hypenPath = resolve(args.path);
        log("Loading:", hypenPath);
        const templateRaw = readFileSync(hypenPath, "utf-8");
        const imports = parseImports(templateRaw);
        const template = removeImports(templateRaw).trim();
        if (imports.length > 0) {
          log("Found imports:", imports);
        }
        const componentName = getComponentName(hypenPath);
        log("Component name:", componentName);
        const modulePath = findModulePath(hypenPath, patterns);
        log("Module path:", modulePath);
        let contents;
        if (modulePath) {
          const relativeModulePath = modulePath.replace(/\.ts$/, ".js");
          contents = `
import _module from "${relativeModulePath}";
export const module = _module;
export const template = ${JSON.stringify(template)};
export const name = ${JSON.stringify(componentName)};
export default { module: _module, template: ${JSON.stringify(template)}, name: ${JSON.stringify(componentName)} };
`;
        } else {
          log("No module found, creating stateless component");
          contents = `
import { app } from "@hypen/core";
const _module = app.defineState({}).build();
export const module = _module;
export const template = ${JSON.stringify(template)};
export const name = ${JSON.stringify(componentName)};
export default { module: _module, template: ${JSON.stringify(template)}, name: ${JSON.stringify(componentName)} };
`;
        }
        return {
          contents,
          loader: "js"
        };
      });
    }
  };
}
var defaultHypenPlugin = hypenPlugin();
function registerHypenPlugin(options) {
  Bun.plugin(hypenPlugin(options));
}
var plugin_default = hypenPlugin;
export {
  registerHypenPlugin,
  hypenPlugin,
  defaultHypenPlugin,
  plugin_default as default
};

//# debugId=F069255D558FE8AD64756E2164756E21
