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 __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
import { Arg, Ctx, Int, Query, Resolver } from '@memberjunction/server';
import { MJUserView_, MJUserViewResolverBase } from '../generated/generated.js';
import { UserResolver } from './UserResolver.js';
import { GetReadOnlyProvider } from '../util.js';
let UserViewResolver = class UserViewResolver extends MJUserViewResolverBase {
    async UserViewsByUserID(UserID, { providers, userPayload }) {
        const provider = GetReadOnlyProvider(providers, { allowFallbackToReadWrite: true });
        return await this.findBy(provider, 'User Views', { UserID }, userPayload.userRecord);
    }
    async DefaultViewByUserAndEntity(UserID, EntityID, { providers, userPayload }) {
        const provider = GetReadOnlyProvider(providers, { allowFallbackToReadWrite: true });
        return await this.findBy(provider, 'User Views', { UserID, EntityID, IsDefault: true }, userPayload.userRecord);
    }
    async CurrentUserDefaultViewByEntityID(EntityID, context) {
        const provider = GetReadOnlyProvider(context.providers, { allowFallbackToReadWrite: true });
        return await this.findBy(provider, 'User Views', {
            UserID: await this.getCurrentUserID(context),
            EntityID,
            IsDefault: true,
        }, context.userPayload.userRecord);
    }
    async getCurrentUserID(context) {
        const userResolver = new UserResolver();
        const user = await userResolver.UserByEmail(context.userPayload.email, context);
        return user.ID;
    }
    async CurrentUserUserViewsByEntityID(EntityID, context) {
        const provider = GetReadOnlyProvider(context.providers, { allowFallbackToReadWrite: true });
        return this.findBy(provider, 'User Views', { UserID: await this.getCurrentUserID(context), EntityID }, context.userPayload.userRecord);
    }
    async UpdateWhereClause(ID, { userPayload, providers }) {
        const p = GetReadOnlyProvider(providers, { allowFallbackToReadWrite: true });
        const u = this.GetUserFromPayload(userPayload);
        const viewEntity = await p.GetEntityObject('User Views', u);
        await viewEntity.Load(ID);
        viewEntity.UpdateWhereClause();
        if (await viewEntity.Save()) {
            return viewEntity.GetAll();
        }
        else {
            throw new Error('Failed to update where clause');
        }
    }
};
__decorate([
    Query(() => [MJUserView_]),
    __param(0, Arg('UserID', () => Int)),
    __param(1, Ctx()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Number, Object]),
    __metadata("design:returntype", Promise)
], UserViewResolver.prototype, "UserViewsByUserID", null);
__decorate([
    Query(() => [MJUserView_]),
    __param(0, Arg('UserID', () => Int)),
    __param(1, Arg('EntityID', () => Int)),
    __param(2, Ctx()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Number, Number, Object]),
    __metadata("design:returntype", Promise)
], UserViewResolver.prototype, "DefaultViewByUserAndEntity", null);
__decorate([
    Query(() => [MJUserView_]),
    __param(0, Arg('EntityID', () => Int)),
    __param(1, Ctx()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Number, Object]),
    __metadata("design:returntype", Promise)
], UserViewResolver.prototype, "CurrentUserDefaultViewByEntityID", null);
__decorate([
    Query(() => [MJUserView_]),
    __param(0, Arg('EntityID', () => Int)),
    __param(1, Ctx()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Number, Object]),
    __metadata("design:returntype", Promise)
], UserViewResolver.prototype, "CurrentUserUserViewsByEntityID", null);
__decorate([
    Query(() => [MJUserView_]),
    __param(0, Arg('ID', () => String)),
    __param(1, Ctx()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object]),
    __metadata("design:returntype", Promise)
], UserViewResolver.prototype, "UpdateWhereClause", null);
UserViewResolver = __decorate([
    Resolver(MJUserView_)
], UserViewResolver);
export { UserViewResolver };
export default UserViewResolver;
//# sourceMappingURL=UserViewResolver.js.map