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 EntityPermissionsEntity_Server_1;
import { RegisterClass } from '@memberjunction/global';
import { BaseEntity } from '@memberjunction/core';
import { EntityPermissionEntity } from '@memberjunction/core-entities';
import axios from 'axios';
import { ___codeGenAPIPort, ___codeGenAPISubmissionDelay, ___codeGenAPIURL } from '../config.js';
let EntityPermissionsEntity_Server = class EntityPermissionsEntity_Server extends EntityPermissionEntity {
    static { EntityPermissionsEntity_Server_1 = this; }
    static _entityIDQueue = [];
    static _lastModifiedTime = null;
    static _submissionTimer = null;
    static _submissionDelay = ___codeGenAPISubmissionDelay;
    static _baseURL = ___codeGenAPIURL;
    static _port = ___codeGenAPIPort;
    static _apiEndpoint = '/api/entity-permissions';
    static getSubmissionURL() {
        return `${this._baseURL}:${this._port}${this._apiEndpoint}`;
    }
    static get EntityIDQueue() {
        return this._entityIDQueue;
    }
    static ClearQueue() {
        this._entityIDQueue = [];
        this._submissionTimer = null;
    }
    static AddToQueue(entityID) {
        if (this._entityIDQueue.indexOf(entityID) === -1)
            this._entityIDQueue.push(entityID);
        this._lastModifiedTime = new Date();
        this.CheckStartSubmissionTimer();
    }
    static CheckStartSubmissionTimer() {
        if (this._submissionTimer === null) {
            this.StartSubmissionTimer();
        }
        else {
            clearTimeout(this._submissionTimer);
            this.StartSubmissionTimer();
        }
    }
    static StartSubmissionTimer() {
        this._submissionTimer = setTimeout(() => {
            this.SubmitQueue();
        }, this._submissionDelay);
    }
    static async SubmitQueue() {
        this._lastModifiedTime = null;
        if (this._entityIDQueue.length > 0) {
            try {
                const response = await axios.post(this.getSubmissionURL(), {
                    entityIDArray: this._entityIDQueue,
                });
                if (response.status === 200 && response.data.status === 'ok') {
                    console.log('Queue submitted successfully.');
                    this.ClearQueue();
                }
                else {
                    console.error('Failed to submit queue:', response.data.errorMessage || 'Unknown error');
                }
            }
            catch (error) {
                console.error('Failed to submit queue:', error);
            }
        }
        else {
            console.log('No entities to submit.');
        }
    }
    Save(options) {
        if (this.Dirty || options?.IgnoreDirtyState)
            EntityPermissionsEntity_Server_1.AddToQueue(this.EntityID);
        return super.Save(options);
    }
    async Delete(options) {
        const success = await super.Delete(options);
        if (success)
            EntityPermissionsEntity_Server_1.AddToQueue(this.EntityID);
        return success;
    }
};
EntityPermissionsEntity_Server = EntityPermissionsEntity_Server_1 = __decorate([
    RegisterClass(BaseEntity, 'Entity Permissions')
], EntityPermissionsEntity_Server);
export { EntityPermissionsEntity_Server };
export function LoadEntityPermissionsServerSubClass() { }
//# sourceMappingURL=entityPermissions.server.js.map