import { LogError, Metadata, RunView } from '@memberjunction/core';
export class ViewOperationsHandler {
    static async runView(params, user) {
        try {
            const md = new Metadata();
            const entity = md.Entities.find(e => e.Name === params.EntityName);
            if (!entity) {
                return {
                    success: false,
                    error: `Entity '${params.EntityName}' not found`
                };
            }
            const permissions = entity.GetUserPermisions(user);
            if (!permissions.CanRead) {
                return {
                    success: false,
                    error: `User ${user.Name} does not have permission to read ${params.EntityName} records`
                };
            }
            this.sanitizeRunViewParams(params);
            const runView = new RunView();
            const result = await runView.RunView(params, user);
            return { success: true, result };
        }
        catch (error) {
            LogError(error);
            return { success: false, error: error?.message || 'Unknown error' };
        }
    }
    static async runViews(paramsArray, user) {
        try {
            const md = new Metadata();
            for (const params of paramsArray) {
                const entity = md.Entities.find(e => e.Name === params.EntityName);
                if (!entity) {
                    return {
                        success: false,
                        error: `Entity '${params.EntityName}' not found`
                    };
                }
                const permissions = entity.GetUserPermisions(user);
                if (!permissions.CanRead) {
                    return {
                        success: false,
                        error: `User ${user.Name} does not have permission to read ${params.EntityName} records`
                    };
                }
                this.sanitizeRunViewParams(params);
            }
            const runView = new RunView();
            const results = await runView.RunViews(paramsArray, user);
            return { success: true, results };
        }
        catch (error) {
            LogError(error);
            return { success: false, error: error?.message || 'Unknown error' };
        }
    }
    static async listEntities(params, user) {
        try {
            const md = new Metadata();
            const entity = md.Entities.find(e => e.Name === params.EntityName);
            if (!entity) {
                throw new Error(`Entity '${params.EntityName}' not found`);
            }
            const permissions = entity.GetUserPermisions(user);
            if (!permissions.CanRead) {
                throw new Error(`User ${user.Name} does not have permission to read ${params.EntityName} records`);
            }
            this.sanitizeRunViewParams(params);
            const runView = new RunView();
            return await runView.RunView(params, user);
        }
        catch (error) {
            LogError(error);
            throw error;
        }
    }
    static async getEntityViews(entityName, user) {
        try {
            const md = new Metadata();
            const entity = md.Entities.find(e => e.Name === entityName);
            if (!entity) {
                return {
                    success: false,
                    error: `Entity '${entityName}' not found`
                };
            }
            const permissions = entity.GetUserPermisions(user);
            if (!permissions.CanRead) {
                return {
                    success: false,
                    error: `User ${user.Name} does not have permission to read ${entityName} records`
                };
            }
            const params = {
                EntityName: 'User Views',
                ExtraFilter: `Entity = '${entityName}'`
            };
            const runView = new RunView();
            const result = await runView.RunView(params, user);
            if (!result.Success) {
                return {
                    success: false,
                    error: result.ErrorMessage || 'Failed to retrieve views'
                };
            }
            const views = result.Results.map(view => ({
                ID: view.ID,
                Name: view.Name,
                Description: view.Description,
                IsShared: view.IsShared,
                CreatedAt: view.CreatedAt
            }));
            return { success: true, views };
        }
        catch (error) {
            LogError(error);
            return { success: false, error: error?.message || 'Unknown error' };
        }
    }
    static sanitizeRunViewParams(params) {
        if (!params.EntityName) {
            throw new Error('EntityName is required');
        }
        if (params.Fields && typeof params.Fields === 'string') {
            params.Fields = params.Fields.split(',');
        }
        if (params.MaxRows !== undefined) {
            params.MaxRows = typeof params.MaxRows === 'string'
                ? parseInt(params.MaxRows)
                : params.MaxRows;
        }
        if (params.StartRow !== undefined) {
            params.StartRow = typeof params.StartRow === 'string'
                ? parseInt(params.StartRow)
                : params.StartRow;
        }
        if (!params.ResultType) {
            params.ResultType = 'simple';
        }
    }
}
//# sourceMappingURL=ViewOperationsHandler.js.map