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;
};
import { BaseEntity, LogError } from "@memberjunction/core";
import { RegisterClass, ValidationErrorInfo } from "@memberjunction/global";
import { MJDashboardEntity } from "../generated/entity_subclasses.js";
import { DashboardEngine } from "../engines/dashboards.js";
let MJDashboardEntityExtended = class MJDashboardEntityExtended extends MJDashboardEntity {
    NewRecord() {
        try {
            super.NewRecord();
            const defaultConfigDetails = {
                columns: 4,
                rowHeight: 150,
                resizable: true,
                reorderable: true,
                items: []
            };
            const configJSON = JSON.stringify(defaultConfigDetails);
            this.Set("UIConfigDetails", configJSON);
            const md = this.ProviderToUse;
            if (md.CurrentUser) {
                this.Set("UserID", md.CurrentUser.ID);
            }
            return true;
        }
        catch (error) {
            LogError("Error in NewRecord: ");
            LogError(error);
            return false;
        }
    }
    /**
     * Override Validate to check dashboard permissions before save.
     * For new records, user must be authenticated.
     * For existing records, user must have edit permission.
     */
    Validate() {
        // Run base validation first
        const result = super.Validate();
        // Check permission for save operation
        const md = this.ProviderToUse;
        const currentUser = this.ContextCurrentUser || md.CurrentUser;
        if (!currentUser) {
            result.Success = false;
            result.Errors.push(new ValidationErrorInfo('Permission', 'You must be logged in to save a dashboard', null));
            return result;
        }
        // For existing records (not new), check edit permission
        if (this.IsSaved) {
            const permissions = DashboardEngine.Instance.GetDashboardPermissions(this.ID, currentUser.ID);
            if (!permissions.CanEdit) {
                result.Success = false;
                result.Errors.push(new ValidationErrorInfo('Permission', 'You do not have permission to edit this dashboard', this.ID));
            }
        }
        return result;
    }
    /**
     * Override Delete to check dashboard permissions before deletion.
     * User must have delete permission (typically only owners can delete).
     */
    async Delete(options) {
        const md = this.ProviderToUse;
        const currentUser = this.ContextCurrentUser || md.CurrentUser;
        if (!currentUser) {
            LogError('Cannot delete dashboard: User not authenticated');
            return false;
        }
        // Check delete permission
        const permissions = DashboardEngine.Instance.GetDashboardPermissions(this.ID, currentUser.ID);
        if (!permissions.CanDelete) {
            LogError(`User ${currentUser.ID} does not have permission to delete dashboard ${this.ID}`);
            return false;
        }
        // Permission granted, proceed with delete
        return super.Delete(options);
    }
};
MJDashboardEntityExtended = __decorate([
    RegisterClass(BaseEntity, 'MJ: Dashboards')
], MJDashboardEntityExtended);
export { MJDashboardEntityExtended };
//# sourceMappingURL=MJDashboardEntityExtended.js.map