"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateBackendE2eService = generateBackendE2eService;
const Generator = __importStar(require("@postxl/generator"));
const schema_1 = require("@postxl/schema");
function generateBackendE2eService(context) {
    const { service } = context.e2eBackend;
    const userModel = context.models.get(schema_1.standardModelName_User);
    if (!userModel || userModel.repository.kind === 'NoRepository') {
        throw new Error(`User model is required for seed migrations and must have a repository!`);
    }
    const imports = Generator.ImportGenerator
        //
        .from(service.location)
        .add(context.actions.dispatcher)
        .addType(context.decoders.dataModel.decodedType)
        .add(context.repositories.dataService)
        .add(userModel.repository)
        .add(context.seed.service)
        .add(context.database.service);
    return `
import { Logger, Provider } from '@nestjs/common'

${imports.generate()}

import { TestConfig_Test } from 'apps/api/src/api.config'

export class ${service.name} {
  private readonly logger = new Logger(${service.name}.name)

  constructor(
    public readonly config: TestConfig_Test,
    private readonly db: ${context.database.service.name},
    private readonly dispatcher: ${context.actions.dispatcher.name},
    private readonly seedService: ${context.seed.service.name},
    private readonly dataService: ${context.repositories.dataService.name},
    private readonly userRepository: ${userModel.repository.name},
  ) {}

  public async resetTestData(data: ${context.decoders.dataModel.decodedType.name}) {
    this.logger.log('✅ Reset test data')

    // empty database
    await this.db.emptyDatabase()
    await this.dataService.init(true)

    // Uncomment this line if the seed data is required for the tests
    // await this.seedService.seed()

    // Seed the database with the provided data
    await this.dispatcher.dispatch({
      action: { scope: 'import', type: 'dataSeed', payload: data },
      user: this.userRepository.rootUser,
    })  
  }
}

export const ${service.factory.name} = (testConfig: TestConfig_Test): Provider<${service.name}> => ({
  provide: ${service.name},
  inject: [${context.database.service.name}, ${context.actions.dispatcher.name}, ${context.seed.service.name}  , ${context.repositories.dataService.name}, ${userModel.repository.name}],
  useFactory: (db: ${context.database.service.name}, dispatcher: ${context.actions.dispatcher.name}, seedService: ${context.seed.service.name}  , data: ${context.repositories.dataService.name}, userRepository: ${userModel.repository.name}) => {
    return new ${service.name}(testConfig, db, dispatcher, seedService, data, userRepository)
  }
})`;
}
//# sourceMappingURL=e2e-service.generator.js.map