"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var Trial_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Trial = void 0;
const crypto_1 = require("crypto");
const Source_1 = require("./Source");
const typeorm_1 = require("typeorm");
// one of the main classes needed to do much of anything
let Trial = Trial_1 = class Trial {
    /*static ensureObject(trial: ITrial): Trial {
        let a = trial as Trial
        if (a.dataFiles)
            return a;
        else
            return new Trial(a)
    }*/
    // filename results - generated from flags of which data to poll + id
    // these files will be deleted after processing, so they may or may not exist. These are their original names however
    get dataFiles() {
        if (this.metadata)
            return {
                items: this.recordItems ? `${this.id}_item.jsonl` : undefined,
                electric: this.recordElectric ? `${this.id}_elec.jsonl` : undefined,
                circuits: this.recordCircuits ? `${this.id}_circ.jsonl` : undefined,
                pollution: this.recordPollution ? `${this.id}_poll.jsonl` : undefined,
            };
        else
            return undefined;
    }
    static ticksToSeconds(ticks) {
        return ticks / 60;
    }
    static ticksToMinutes(ticks) {
        return ticks / 3600;
    }
    static minutesToTicks(minutes) {
        return minutes * 3600;
    }
    static secondsToTicks(seconds) {
        return seconds * 60;
    }
    static timeToTicks(minutes, seconds = 0) {
        return (this.minutesToTicks(minutes) + this.secondsToTicks(seconds));
    }
    constructor(params = null) {
        var _a, _b, _c;
        // text result (raw) system data
        this.rawSystemText = null;
        this.itemMetadata = null;
        this.electricMetadata = null;
        this.circuitMetadata = null;
        this.pollutionMetadata = null;
        this.systemMetadata = null;
        // we allow no-param construction of any class - this helps with ORMs and type casting
        if (!params)
            return;
        // In the event the constructor was called null, we will allow and still create an ID. ORMs can do this at times
        this.id = (_a = params === null || params === void 0 ? void 0 : params.id) !== null && _a !== void 0 ? _a : (0, crypto_1.randomUUID)();
        this.createdAt = new Date();
        // If our source is a string, we need to create the source object automatically
        if (params.source && typeof params.source == 'string') {
            if (params.source.endsWith('.zip')) {
                // create save source
                this.source = new Source_1.Source({
                    text: params.source,
                    variant: 'savegame'
                });
            }
            else {
                // create blueprint source
                this.source = new Source_1.Source({
                    text: params.source,
                    variant: 'blueprint'
                });
            }
        }
        else if (params.source) {
            // otherwise, we assume its already a source object
            this.source = Source_1.Source.ensureObject(params.source);
        }
        // set the other data we need as well about the trial
        this.length = (_b = params.length) !== null && _b !== void 0 ? _b : 7200;
        this.tickInterval = (_c = params.tickInterval) !== null && _c !== void 0 ? _c : 60;
        if (params.initialBots)
            this.initialBots = params.initialBots;
        this.recordItems = (params.recordItems != undefined) ? params.recordItems : false;
        this.recordElectric = (params.recordElectric != undefined) ? params.recordElectric : false;
        this.recordCircuits = (params.recordCircuits != undefined) ? params.recordCircuits : false;
        this.recordPollution = (params.recordPollution != undefined) ? params.recordPollution : false;
        this.recordSystem = (params.recordSystem != undefined) ? params.recordSystem : false;
        // set the name and desc. Otherwise, will be generated based on settings
        if (params.name)
            this.name = params.name;
        else {
            this.name = `Trial of BPLength ${this.source.text.length} for [${this.recordItems ? 'I' : ''}${this.recordElectric ? 'E' : ''}${this.recordCircuits ? 'C' : ''}${this.recordPollution ? 'P' : ''}${this.recordSystem ? 'S' : ''}]`;
        }
        if (params.desc)
            this.desc = params.desc;
        else {
            this.desc = `[${this.recordItems ? 'I' : ''}${this.recordElectric ? 'E' : ''}${this.recordCircuits ? 'C' : ''}${this.recordPollution ? 'P' : ''}${this.recordSystem ? 'S' : ''}]
            In-Game Length ${Trial_1.ticksToSeconds(this.length)} Interval ${Trial_1.ticksToSeconds(this.tickInterval)}`;
        }
        this.stage = "new";
    }
    setStage(stage) {
        this.stage = stage;
    }
};
exports.Trial = Trial;
__decorate([
    (0, typeorm_1.PrimaryColumn)(),
    __metadata("design:type", String)
], Trial.prototype, "id", void 0);
__decorate([
    (0, typeorm_1.Column)({
        nullable: false,
        type: 'varchar'
    }),
    __metadata("design:type", String)
], Trial.prototype, "stage", void 0);
__decorate([
    (0, typeorm_1.ManyToOne)(() => Source_1.Source),
    __metadata("design:type", Source_1.Source)
], Trial.prototype, "source", void 0);
__decorate([
    (0, typeorm_1.Column)({
        nullable: false,
    }),
    __metadata("design:type", Number)
], Trial.prototype, "length", void 0);
__decorate([
    (0, typeorm_1.Column)({
        name: 'tick_interval',
        nullable: false,
    }),
    __metadata("design:type", Number)
], Trial.prototype, "tickInterval", void 0);
__decorate([
    (0, typeorm_1.Column)({
        name: 'initial_bots',
        nullable: false,
    }),
    __metadata("design:type", Number)
], Trial.prototype, "initialBots", void 0);
__decorate([
    (0, typeorm_1.Column)({
        name: 'record_items',
        nullable: false
    }),
    __metadata("design:type", Boolean)
], Trial.prototype, "recordItems", void 0);
__decorate([
    (0, typeorm_1.Column)({
        name: 'record_electric',
        nullable: false,
    }),
    __metadata("design:type", Boolean)
], Trial.prototype, "recordElectric", void 0);
__decorate([
    (0, typeorm_1.Column)({
        name: 'record_circuits',
        nullable: false,
    }),
    __metadata("design:type", Boolean)
], Trial.prototype, "recordCircuits", void 0);
__decorate([
    (0, typeorm_1.Column)({
        name: 'record_pollution',
        nullable: false,
    }),
    __metadata("design:type", Boolean)
], Trial.prototype, "recordPollution", void 0);
__decorate([
    (0, typeorm_1.Column)({
        name: 'record_system',
        nullable: false,
    }),
    __metadata("design:type", Boolean)
], Trial.prototype, "recordSystem", void 0);
__decorate([
    (0, typeorm_1.Column)({
        nullable: true,
    }),
    __metadata("design:type", String)
], Trial.prototype, "name", void 0);
__decorate([
    (0, typeorm_1.Column)({
        nullable: true,
    }),
    __metadata("design:type", String)
], Trial.prototype, "desc", void 0);
__decorate([
    (0, typeorm_1.Column)({
        name: 'created_at',
        nullable: true,
    }),
    __metadata("design:type", Date)
], Trial.prototype, "createdAt", void 0);
__decorate([
    (0, typeorm_1.Column)({
        name: 'started_run_at',
        nullable: true
    }),
    __metadata("design:type", Date)
], Trial.prototype, "startedRunAt", void 0);
__decorate([
    (0, typeorm_1.Column)({
        name: 'started_at',
        nullable: true
    }),
    __metadata("design:type", Date)
], Trial.prototype, "startedAt", void 0);
__decorate([
    (0, typeorm_1.Column)({
        name: 'ended_at',
        nullable: true
    }),
    __metadata("design:type", Date
    // text result of trial - logs and/or system data
    )
], Trial.prototype, "endedAt", void 0);
__decorate([
    (0, typeorm_1.Column)({
        nullable: true,
        name: 'text_logs',
        type: 'simple-array'
    }),
    __metadata("design:type", Array)
], Trial.prototype, "textLogs", void 0);
__decorate([
    (0, typeorm_1.Column)({
        nullable: true,
        type: 'simple-json'
    }),
    __metadata("design:type", Object)
], Trial.prototype, "metadata", void 0);
__decorate([
    (0, typeorm_1.Column)({
        nullable: true,
        name: 'item_metadata',
        type: 'simple-json'
    }),
    __metadata("design:type", Object)
], Trial.prototype, "itemMetadata", void 0);
__decorate([
    (0, typeorm_1.Column)({
        nullable: true,
        name: 'electric_metadata',
        type: 'simple-json'
    }),
    __metadata("design:type", Object)
], Trial.prototype, "electricMetadata", void 0);
__decorate([
    (0, typeorm_1.Column)({
        nullable: true,
        name: 'circuit_metadata',
        type: 'simple-json'
    }),
    __metadata("design:type", Object)
], Trial.prototype, "circuitMetadata", void 0);
__decorate([
    (0, typeorm_1.Column)({
        nullable: true,
        name: 'pollution_metadata',
        type: 'simple-json'
    }),
    __metadata("design:type", Object)
], Trial.prototype, "pollutionMetadata", void 0);
__decorate([
    (0, typeorm_1.Column)({
        nullable: true,
        name: 'system_metadata',
        type: 'simple-json'
    }),
    __metadata("design:type", Object)
], Trial.prototype, "systemMetadata", void 0);
exports.Trial = Trial = Trial_1 = __decorate([
    (0, typeorm_1.Entity)('trial'),
    __metadata("design:paramtypes", [Object])
], Trial);
//# sourceMappingURL=Trial.js.map