"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var chunk_RGXJYLMD_exports = {};
__export(chunk_RGXJYLMD_exports, {
  Migrate: () => Migrate
});
module.exports = __toCommonJS(chunk_RGXJYLMD_exports);
var import_chunk_GGA2F64G = require("./chunk-GGA2F64G.js");
var import_chunk_23DZXSTI = require("./chunk-23DZXSTI.js");
var import_chunk_6DDIA3ZD = require("./chunk-6DDIA3ZD.js");
var import_internals = require("@prisma/internals");
var Migrate = class _Migrate {
  engine;
  schemaContext;
  schemaFilter;
  shadowDbInitScript;
  migrationsDirectoryPath;
  constructor({ schemaContext, migrationsDirPath, engine, schemaFilter, shadowDbInitScript }) {
    this.engine = engine;
    this.schemaContext = schemaContext;
    this.migrationsDirectoryPath = migrationsDirPath;
    this.schemaFilter = schemaFilter ?? { externalTables: [], externalEnums: [] };
    this.shadowDbInitScript = shadowDbInitScript ?? "";
  }
  static async setup({ schemaContext, schemaEngineConfig, ...rest }) {
    const schemaEngine = await (async () => {
      const datasource = schemaEngineConfig.datasource;
      return await import_chunk_6DDIA3ZD.SchemaEngineCLI.setup({ datasource, schemaContext, ...rest });
    })();
    return new _Migrate({ engine: schemaEngine, schemaContext, ...rest });
  }
  async stop() {
    await this.engine.stop();
  }
  getPrismaSchema() {
    if (!this.schemaContext) throw new Error("this.schemaContext is undefined");
    return (0, import_internals.toSchemasContainer)(this.schemaContext.schemaFiles);
  }
  reset() {
    return this.engine.reset({
      filter: this.schemaFilter
    });
  }
  async createMigration(params) {
    if (!this.migrationsDirectoryPath) throw new Error("this.migrationsDirectoryPath is undefined");
    const migrationsList = await (0, import_chunk_GGA2F64G.listMigrations)(this.migrationsDirectoryPath, this.shadowDbInitScript);
    const { connectorType, generatedMigrationName, extension, migrationScript } = await this.engine.createMigration({
      ...params,
      migrationsList,
      filters: this.schemaFilter
    });
    const { baseDir, lockfile } = migrationsList;
    if (migrationScript === null) {
      return {
        generatedMigrationName: void 0
      };
    }
    const directoryPath = await (0, import_chunk_23DZXSTI.createMigration)({
      baseDir,
      generatedMigrationName
    }).catch((e) => {
      throw new Error(`Failed to create a new migration directory: ${e.message}`);
    });
    await (0, import_chunk_23DZXSTI.writeMigrationScript)({
      baseDir,
      extension,
      migrationName: generatedMigrationName,
      script: migrationScript
    }).catch((e) => {
      throw new Error(`Failed to write migration script to ${directoryPath}: ${e.message}`);
    });
    await (0, import_chunk_23DZXSTI.writeMigrationLockfile)({
      baseDir,
      connectorType,
      lockfile
    }).catch((e) => {
      throw new Error(`Failed to write the migration lock file to ${baseDir}: ${e.message}`);
    });
    return {
      generatedMigrationName
    };
  }
  async diagnoseMigrationHistory({
    optInToShadowDatabase
  }) {
    if (!this.migrationsDirectoryPath) throw new Error("this.migrationsDirectoryPath is undefined");
    const migrationsList = await (0, import_chunk_GGA2F64G.listMigrations)(this.migrationsDirectoryPath, this.shadowDbInitScript);
    return this.engine.diagnoseMigrationHistory({
      migrationsList,
      optInToShadowDatabase,
      filters: this.schemaFilter
    });
  }
  async listMigrationDirectories() {
    if (!this.migrationsDirectoryPath) throw new Error("this.migrationsDirectoryPath is undefined");
    const migrationsList = await (0, import_chunk_GGA2F64G.listMigrations)(this.migrationsDirectoryPath, this.shadowDbInitScript);
    return {
      migrations: migrationsList.migrationDirectories.map((dir) => dir.path)
    };
  }
  async devDiagnostic() {
    if (!this.migrationsDirectoryPath) throw new Error("this.migrationsDirectoryPath is undefined");
    const migrationsList = await (0, import_chunk_GGA2F64G.listMigrations)(this.migrationsDirectoryPath, this.shadowDbInitScript);
    return this.engine.devDiagnostic({
      migrationsList,
      filters: this.schemaFilter
    });
  }
  async markMigrationApplied({ migrationId }) {
    if (!this.migrationsDirectoryPath) throw new Error("this.migrationsDirectoryPath is undefined");
    const migrationsList = await (0, import_chunk_GGA2F64G.listMigrations)(this.migrationsDirectoryPath, this.shadowDbInitScript);
    return await this.engine.markMigrationApplied({
      migrationsList,
      migrationName: migrationId
    });
  }
  markMigrationRolledBack({ migrationId }) {
    return this.engine.markMigrationRolledBack({
      migrationName: migrationId
    });
  }
  async applyMigrations() {
    if (!this.migrationsDirectoryPath) throw new Error("this.migrationsDirectoryPath is undefined");
    const migrationsList = await (0, import_chunk_GGA2F64G.listMigrations)(this.migrationsDirectoryPath, this.shadowDbInitScript);
    return this.engine.applyMigrations({
      migrationsList,
      filters: this.schemaFilter
    });
  }
  async evaluateDataLoss() {
    if (!this.migrationsDirectoryPath) throw new Error("this.migrationsDirectoryPath is undefined");
    const migrationsList = await (0, import_chunk_GGA2F64G.listMigrations)(this.migrationsDirectoryPath, this.shadowDbInitScript);
    const schema = this.getPrismaSchema();
    return this.engine.evaluateDataLoss({
      migrationsList,
      schema,
      filters: this.schemaFilter
    });
  }
  async push({ force = false }) {
    const schema = this.getPrismaSchema();
    const { warnings, unexecutable, executedSteps } = await this.engine.schemaPush({
      force,
      schema,
      filters: this.schemaFilter
    });
    return {
      executedSteps,
      warnings,
      unexecutable
    };
  }
};
