import { getUnbuildLoader } from './chunk-Q3I5ZHAF.js';
import { installPackage } from './chunk-QLRH4TNM.js';
import { createLog } from './chunk-EAGALHEL.js';
import { writeFile } from './chunk-V3VWCAQH.js';
import { __name } from './chunk-6AXVLTU5.js';
import { LogLevelLabel } from '@storm-software/config-tools/types';
import { build } from '@storm-software/unbuild';
import { defu } from 'defu';

var Plugin = class {
  static {
    __name(this, "Plugin");
  }
  /**
  * The name of the plugin
  */
  name;
  /**
  * The name of the plugin
  */
  installPath;
  /**
  * The logger function to use
  */
  log;
  /**
  * The constructor for the plugin
  *
  * @param name - The name of the plugin
  * @param installPath - The path to install the plugin
  */
  constructor(name, installPath) {
    this.name = name.toLowerCase();
    if (this.name.endsWith("plugin")) {
      this.name = this.name.replace(/plugin$/, "").trim();
    }
    this.log = createLog(`${this.name}-plugin`);
    this.installPath = installPath || `@storm-stack/plugin-${this.name}`;
    if (!installPath) {
      this.log(LogLevelLabel.WARN, `No install path parameter provided to constructor for ${this.name} plugin. Will attempt to use "${this.installPath}".`);
    }
  }
  /**
  * Writes a file to the file system
  *
  * @param filepath - The file path to write the file
  * @param content - The content to write to the file
  * @param skipFormat - Should the plugin skip formatting the `content` string with Prettier
  */
  async writeFile(filepath, content, skipFormat = false) {
    this.log(LogLevelLabel.TRACE, `Writing file ${filepath} to disk`);
    return writeFile(this.log, filepath, content, skipFormat);
  }
  /**
  * Installs a package if it is not already installed.
  *
  * @param context - The resolved Storm Stack context
  * @param packageName - The name of the package to install
  * @param dev - Whether to install the package as a dev dependency
  */
  async install(context, packageName, dev = false) {
    return installPackage(this.log, context, packageName, dev);
  }
  /**
  * Builds the Storm Stack project
  *
  * @param context - The resolved Storm Stack context
  */
  async build(context) {
    this.log(LogLevelLabel.TRACE, "Building Storm Stack project");
    if (context.projectType === "library" || context.projectType === "adapter") {
      await this.buildLib(context);
    } else {
      await this.buildApp(context);
    }
  }
  /**
  * Run the build process for application projects
  *
  * @param _context - The resolved Storm Stack context
  */
  async buildApp(_context) {
    this.log(LogLevelLabel.WARN, "Skipping build for project - this plugin does not support application builds");
  }
  /**
  * Run the unbuild process for library builds
  *
  * @param context - The unbuild context
  */
  async buildLib(context) {
    return build(defu(context.override, {
      projectRoot: context.projectRoot,
      outputPath: context.outputPath || "dist",
      platform: context.platform,
      generatePackageJson: true,
      minify: Boolean(context.minify),
      sourcemap: context.mode === "production",
      loaders: [
        getUnbuildLoader(context)
      ],
      env: context.resolvedDotenv.values
    }));
  }
};

export { Plugin };
//# sourceMappingURL=chunk-INBXDZHG.js.map
//# sourceMappingURL=chunk-INBXDZHG.js.map