export const Kind = 'Core#ProjectExecution';
export class ProjectExecution {
    kind = Kind;
    /**
     * The key of the execution entry. This is generated by the store. Client value is ignored.
     */
    key = '';
    /**
     * The key of the parent project.
     */
    project = '';
    /**
     * The key of the parent folder, if any.
     */
    parent;
    /**
     * The application id that generated the entry.
     */
    app = '';
    /**
     * The user id that made that request.
     * This is only available when the store support multi-user environment.
     */
    user;
    /**
     * @param value The timestamp of the midnight when the request object was updated
     */
    set midnight(value) {
        if (!value) {
            this.midnightInternal = this.defaultMidnight();
        }
        else {
            this.midnightInternal = value;
        }
    }
    /**
     * @returns The timestamp of the midnight when the request object was updated
     */
    get midnight() {
        if (this.midnightInternal) {
            return this.midnightInternal;
        }
        return this.defaultMidnight();
    }
    midnightInternal = 0;
    /**
     * The timestamp when the execution started
     */
    started = 0;
    /**
     * The timestamp when the execution ended
     */
    ended = 0;
    /**
     * The execution logs for each iteration.
     */
    iterations = [];
    constructor(input) {
        let init;
        if (typeof input === 'string') {
            init = JSON.parse(input);
        }
        else if (typeof input === 'object') {
            init = input;
        }
        else {
            const now = Date.now();
            init = {
                kind: Kind,
                user: '',
                app: '',
                key: '',
                project: '',
                ended: 0,
                iterations: [],
                midnight: 0,
                started: now,
            };
        }
        this.new(init);
    }
    new(init) {
        const { started = Date.now(), midnight, project, user, key, app, ended = started, iterations, parent } = init;
        this.started = started;
        this.ended = ended;
        this.iterations = iterations.map((i) => structuredClone(i));
        if (key) {
            this.key = key;
        }
        else {
            this.key = '';
        }
        if (app) {
            this.app = app;
        }
        else {
            this.app = '';
        }
        if (midnight) {
            this.midnight = midnight;
        }
        if (project) {
            this.project = project;
        }
        else {
            this.project = '';
        }
        if (parent) {
            this.parent = parent;
        }
        else {
            this.parent = '';
        }
        if (user) {
            this.user = user;
        }
        else {
            this.user = '';
        }
    }
    toJSON() {
        const result = {
            kind: Kind,
            midnight: this.midnight,
            user: this.user,
            key: this.key,
            project: this.project,
            app: this.app,
            ended: this.ended,
            iterations: this.iterations.map((i) => structuredClone(i)),
            started: this.started,
        };
        if (this.parent) {
            result.parent = this.parent;
        }
        return result;
    }
    /**
     * @returns The default value for the midnight when the request was last updated.
     */
    defaultMidnight() {
        const d = new Date(this.started);
        d.setHours(0, 0, 0, 0);
        return d.getTime();
    }
}
//# sourceMappingURL=ProjectExecution.js.map