"use strict";
/*
 * @adonisjs/lucid
 *
 * (c) Harminder Virk <virk@adonisjs.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.SeedsRunner = void 0;
const SeedersSource_1 = require("./SeedersSource");
/**
 * Seeds Runner exposes the API to traverse seeders and execute them
 * in bulk
 */
class SeedsRunner {
    constructor(db, app, connectionName) {
        Object.defineProperty(this, "db", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: db
        });
        Object.defineProperty(this, "app", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: app
        });
        Object.defineProperty(this, "connectionName", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: connectionName
        });
        Object.defineProperty(this, "client", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: this.db.connection(this.connectionName || this.db.primaryConnectionName)
        });
        Object.defineProperty(this, "config", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: this.db.getRawConnection(this.client.connectionName).config
        });
    }
    /**
     * Returns the seeder source by ensuring value is a class constructor
     */
    async getSeederSource(file) {
        const source = await file.getSource();
        if (typeof source === 'function') {
            return source;
        }
        throw new Error(`Invalid schema class exported by "${file.name}"`);
    }
    /**
     * Returns an array of seeders
     */
    async getList() {
        return new SeedersSource_1.SeedersSource(this.config, this.app).getSeeders();
    }
    /**
     * Executes the seeder
     */
    async run(file) {
        const Source = await this.getSeederSource(file);
        const seeder = {
            status: 'pending',
            file: file,
        };
        if ('developmentOnly' in Source) {
            this.app.logger.warn(`Seeder "${file.name}" is using the deprecated flag "developmentOnly".`);
        }
        /**
         * Ignore when when the node environement is not the same as the seeder configuration.
         */
        if ((Source.developmentOnly && !this.app.inDev) ||
            (Source.environment && !Source.environment.includes(this.app.nodeEnvironment))) {
            seeder.status = 'ignored';
            return seeder;
        }
        try {
            const seederInstance = new Source(this.client);
            if (typeof seederInstance.run !== 'function') {
                throw new Error(`Missing method "run" on "${seeder.file.name}" seeder`);
            }
            await seederInstance.run();
            seeder.status = 'completed';
        }
        catch (error) {
            seeder.status = 'failed';
            seeder.error = error;
        }
        return seeder;
    }
    /**
     * Close database connections
     */
    async close() {
        await this.db.manager.closeAll(true);
    }
}
exports.SeedsRunner = SeedsRunner;
