import { RequestLog, Kind as RequestLogKind } from './RequestLog.js';
export const Kind = 'Core#HttpHistory';
export const createdSymbol = Symbol('created');
export const midnightSymbol = Symbol('midnight');
/**
 * An HTTP history is an object containing an information of a request and response
 * made by an application.
 *
 * Note, history object are not mutable. Can only be created or deleted.
 */
export class HttpHistory {
    [createdSymbol] = 0;
    [midnightSymbol] = 0;
    kind = Kind;
    /**
     * The key of the history entry. The application must
     * revalidate the local copy of the record as the store may change the key.
     */
    key = '';
    /**
     * The key of the parent organization.
     */
    organization = '';
    /**
     * The key of the project the request belongs to.
     */
    project = '';
    /**
     * The application id that generated the history entry.
     */
    app = '';
    /**
     * The user id that made that request.
     * This is only available when the store support multi-user environment.
     */
    user = '';
    /**
     * The key of the request that generated the history entry.
     */
    request = '';
    /**
     * The execution log of the HTTP request with a response.
     */
    log = new RequestLog();
    /**
     * @param value The timestamp when the request was created.
     */
    set created(value) {
        if (!value) {
            this[createdSymbol] = Date.now();
        }
        else {
            this[createdSymbol] = value;
        }
        const d = new Date(this[createdSymbol]);
        d.setHours(0, 0, 0, 0);
        this[midnightSymbol] = d.getTime();
    }
    /**
     * @returns The timestamp when the request was created.
     */
    get created() {
        return this[createdSymbol] || Date.now();
    }
    /**
     * @param value The timestamp of the midnight when the request object was updated
     */
    set midnight(value) {
        if (!value) {
            this[midnightSymbol] = this.defaultMidnight();
        }
        else {
            this[midnightSymbol] = value;
        }
    }
    /**
     * @returns The timestamp of the midnight when the request object was updated
     */
    get midnight() {
        if (this[midnightSymbol]) {
            return this[midnightSymbol];
        }
        return this.defaultMidnight();
    }
    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,
                created: now,
                user: '',
                app: '',
                key: '',
                project: '',
                request: '',
                organization: '',
                log: {
                    kind: RequestLogKind,
                },
            };
        }
        this.new(init);
    }
    new(init) {
        const { log, created = Date.now(), midnight, organization, project, request, user, key, app } = init;
        this.log = new RequestLog(log);
        this.created = created;
        if (key) {
            this.key = key;
        }
        else {
            this.key = '';
        }
        if (app) {
            this.app = app;
        }
        else {
            this.app = '';
        }
        if (midnight) {
            this.midnight = midnight;
        }
        if (organization) {
            this.organization = organization;
        }
        else {
            this.organization = '';
        }
        if (project) {
            this.project = project;
        }
        else {
            this.project = '';
        }
        if (request) {
            this.request = request;
        }
        else {
            this.request = '';
        }
        if (user) {
            this.user = user;
        }
        else {
            this.user = '';
        }
    }
    toJSON() {
        const result = {
            kind: Kind,
            created: this.created,
            midnight: this.midnight,
            log: this.log.toJSON(),
            user: this.user,
            key: this.key,
            organization: this.organization,
            project: this.project,
            request: this.request,
            app: this.app,
        };
        return result;
    }
    /**
     * @returns The default value for the midnight when the request was last updated.
     */
    defaultMidnight() {
        const d = new Date(this.created);
        d.setHours(0, 0, 0, 0);
        return d.getTime();
    }
}
//# sourceMappingURL=HttpHistory.js.map