"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var module_exports = {};
__export(module_exports, {
  Module: () => Module,
  ReExportTypeModule: () => ReExportTypeModule,
  ReExportVariableModule: () => ReExportVariableModule,
  SingleFileModule: () => SingleFileModule
});
module.exports = __toCommonJS(module_exports);
var import_path = require("path");
var utils = __toESM(require("../utils"));
var consts = __toESM(require("./consts"));
var strings = __toESM(require("./strings"));
class Module {
  constructor(_def) {
    this._def = _def;
  }
  _localDependencies = [];
  _customTypeName;
  get path() {
    return this._def.path.split(import_path.posix.sep).map(strings.fileName).join(import_path.posix.sep);
  }
  /**
   * @returns module name (equivalent to unescaped file name without extension)
   */
  get name() {
    const path = this._def.path;
    const basename = import_path.posix.basename(path);
    if (basename === consts.INDEX_FILE || basename === consts.INDEX_DECLARATION_FILE) {
      const dirPath = import_path.posix.dirname(path);
      const dirname = import_path.posix.basename(dirPath);
      return dirname;
    }
    const withoutExtension = utils.path.rmExtension(basename);
    return withoutExtension;
  }
  get isDefaultExport() {
    return this._def.exportName === consts.DEFAULT_EXPORT_NAME;
  }
  get exportName() {
    return this._def.exportName;
  }
  get deps() {
    return [...this._localDependencies];
  }
  get typeName() {
    return this._customTypeName ?? this.name;
  }
  get importAlias() {
    return this.typeName.split(import_path.posix.sep).map(strings.importAlias).join("__");
  }
  setCustomTypeName(alias) {
    this._customTypeName = alias;
    return this;
  }
  pushDep(...dependencies) {
    this._localDependencies.push(...dependencies);
    return this;
  }
  unshift(...basePath) {
    this._def = {
      ...this._def,
      path: import_path.posix.join(...basePath, this._def.path)
    };
    this._localDependencies = this._localDependencies.map((d) => d.unshift(...basePath));
    return this;
  }
  async toFile() {
    return {
      path: this.path,
      content: await this.getContent()
    };
  }
  async flatten() {
    const self = await this.toFile();
    const allFiles = [self];
    for (const dep of this._localDependencies) {
      const depFiles = await dep.flatten();
      allFiles.push(...depFiles);
    }
    return allFiles;
  }
  import(base) {
    let relativePath = import_path.posix.relative(import_path.posix.dirname(base.path), this.path);
    relativePath = import_path.posix.join(".", relativePath);
    return utils.path.rmExtension(relativePath);
  }
}
class ReExportTypeModule extends Module {
  constructor(def) {
    super({
      ...def,
      path: consts.INDEX_FILE
    });
  }
  async getContent() {
    let content = consts.GENERATED_HEADER;
    for (const m of this.deps) {
      const { importAlias } = m;
      const importFrom = m.import(this);
      content += `import * as ${importAlias} from "./${importFrom}";
`;
      content += `export * as ${importAlias} from "./${importFrom}";
`;
    }
    content += "\n";
    content += `export type ${this.exportName} = {
`;
    for (const { importAlias, typeName, exportName: exports2 } of this.deps) {
      content += `  "${typeName}": ${importAlias}.${exports2};
`;
    }
    content += "}";
    content += "\n";
    return content;
  }
}
class ReExportVariableModule extends Module {
  _extraProps = {};
  constructor(def) {
    super({
      ...def,
      path: consts.INDEX_FILE
    });
    this._extraProps = def.extraProps ?? {};
  }
  async getContent() {
    let content = consts.GENERATED_HEADER;
    for (const m of this.deps) {
      const { importAlias } = m;
      const importFrom = m.import(this);
      content += `import * as ${importAlias} from "./${importFrom}";
`;
      content += `export * as ${importAlias} from "./${importFrom}";
`;
    }
    content += "\n";
    const depProps = Object.fromEntries(
      this.deps.map(({ name, exportName, importAlias }) => [name, `${importAlias}.${exportName}`])
    );
    const allProps = { ...depProps, ...this._extraProps };
    content += `export const ${this.exportName} = {
`;
    for (const [key, value] of Object.entries(allProps)) {
      content += `  "${key}": ${value},
`;
    }
    content += "}";
    content += "\n";
    return content;
  }
}
class SingleFileModule extends Module {
  _content;
  constructor(def) {
    super(def);
    this._content = def.content;
  }
  async getContent() {
    return this._content;
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  Module,
  ReExportTypeModule,
  ReExportVariableModule,
  SingleFileModule
});
//# sourceMappingURL=module.js.map
