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 { CompositeKey } from '@memberjunction/core';
import { Arg, CompositeKeyInputType, CompositeKeyOutputType, Ctx, Field, InputType, Int, Mutation, ObjectType, Query, Resolver, } from '@memberjunction/server';
import { UserCache } from '@memberjunction/sqlserver-dataprovider';
import { MJUserFavorite_, MJUserFavoriteResolverBase } from '../generated/generated.js';
import { GetReadOnlyProvider } from '../util.js';
let UserFavoriteSearchParams = class UserFavoriteSearchParams {
    EntityID;
    CompositeKey;
    UserID;
};
__decorate([
    Field(() => String),
    __metadata("design:type", String)
], UserFavoriteSearchParams.prototype, "EntityID", void 0);
__decorate([
    Field(() => CompositeKeyInputType),
    __metadata("design:type", CompositeKeyInputType)
], UserFavoriteSearchParams.prototype, "CompositeKey", void 0);
__decorate([
    Field(() => String),
    __metadata("design:type", String)
], UserFavoriteSearchParams.prototype, "UserID", void 0);
UserFavoriteSearchParams = __decorate([
    InputType()
], UserFavoriteSearchParams);
export { UserFavoriteSearchParams };
let UserFavoriteSetParams = class UserFavoriteSetParams {
    EntityID;
    CompositeKey;
    UserID;
    IsFavorite;
};
__decorate([
    Field(() => String),
    __metadata("design:type", String)
], UserFavoriteSetParams.prototype, "EntityID", void 0);
__decorate([
    Field(() => CompositeKeyInputType),
    __metadata("design:type", CompositeKeyInputType)
], UserFavoriteSetParams.prototype, "CompositeKey", void 0);
__decorate([
    Field(() => String),
    __metadata("design:type", String)
], UserFavoriteSetParams.prototype, "UserID", void 0);
__decorate([
    Field(() => Boolean),
    __metadata("design:type", Boolean)
], UserFavoriteSetParams.prototype, "IsFavorite", void 0);
UserFavoriteSetParams = __decorate([
    InputType()
], UserFavoriteSetParams);
export { UserFavoriteSetParams };
let UserFavoriteResult = class UserFavoriteResult {
    EntityID;
    CompositeKey;
    UserID;
    IsFavorite;
    Success;
};
__decorate([
    Field(() => Int),
    __metadata("design:type", Number)
], UserFavoriteResult.prototype, "EntityID", void 0);
__decorate([
    Field(() => CompositeKeyOutputType),
    __metadata("design:type", CompositeKeyOutputType)
], UserFavoriteResult.prototype, "CompositeKey", void 0);
__decorate([
    Field(() => Int),
    __metadata("design:type", Number)
], UserFavoriteResult.prototype, "UserID", void 0);
__decorate([
    Field(() => Boolean),
    __metadata("design:type", Boolean)
], UserFavoriteResult.prototype, "IsFavorite", void 0);
__decorate([
    Field(() => Boolean),
    __metadata("design:type", Boolean)
], UserFavoriteResult.prototype, "Success", void 0);
UserFavoriteResult = __decorate([
    ObjectType()
], UserFavoriteResult);
export { UserFavoriteResult };
let UserFavoriteResolver = class UserFavoriteResolver extends MJUserFavoriteResolverBase {
    async UserFavoritesByUserID(UserID, { providers, userPayload }) {
        const provider = GetReadOnlyProvider(providers, { allowFallbackToReadWrite: true });
        return await this.findBy(provider, 'User Favorites', { UserID }, userPayload.userRecord);
    }
    async UserFavoriteSearchByParams(params, { providers, userPayload }) {
        const provider = GetReadOnlyProvider(providers, { allowFallbackToReadWrite: true });
        return await this.findBy(provider, 'User Favorites', params, userPayload.userRecord);
    }
    async GetRecordFavoriteStatus(params, { providers, userPayload }) {
        const p = GetReadOnlyProvider(providers, { allowFallbackToReadWrite: true });
        const pk = new CompositeKey(params.CompositeKey.KeyValuePairs);
        const e = p.Entities.find((e) => e.ID === params.EntityID);
        if (e)
            return {
                EntityID: params.EntityID,
                UserID: params.UserID,
                CompositeKey: pk,
                IsFavorite: await p.GetRecordFavoriteStatus(params.UserID, e.Name, pk, userPayload.userRecord),
                Success: true,
            };
        else
            throw new Error(`Entity ID:${params.EntityID} not found`);
    }
    async SetRecordFavoriteStatus(params, { userPayload, providers }) {
        const p = GetReadOnlyProvider(providers, { allowFallbackToReadWrite: true });
        const pk = new CompositeKey(params.CompositeKey.KeyValuePairs);
        const e = p.Entities.find((e) => e.ID === params.EntityID);
        const u = UserCache.Users.find((u) => u.ID === userPayload.userRecord.ID);
        if (e) {
            await p.SetRecordFavoriteStatus(params.UserID, e.Name, pk, params.IsFavorite, u);
            return {
                Success: true,
                EntityID: params.EntityID,
                UserID: params.UserID,
                CompositeKey: params.CompositeKey,
                IsFavorite: params.IsFavorite,
            };
        }
        else
            throw new Error(`Entity ID:${params.EntityID} not found`);
    }
};
__decorate([
    Query(() => [MJUserFavorite_]),
    __param(0, Arg('UserID', () => Int)),
    __param(1, Ctx()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Number, Object]),
    __metadata("design:returntype", Promise)
], UserFavoriteResolver.prototype, "UserFavoritesByUserID", null);
__decorate([
    Query(() => [MJUserFavorite_]),
    __param(0, Arg('params', () => Int)),
    __param(1, Ctx()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [UserFavoriteSearchParams, Object]),
    __metadata("design:returntype", Promise)
], UserFavoriteResolver.prototype, "UserFavoriteSearchByParams", null);
__decorate([
    Query(() => UserFavoriteResult),
    __param(0, Arg('params', () => UserFavoriteSearchParams)),
    __param(1, Ctx()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [UserFavoriteSearchParams, Object]),
    __metadata("design:returntype", Promise)
], UserFavoriteResolver.prototype, "GetRecordFavoriteStatus", null);
__decorate([
    Mutation(() => UserFavoriteResult),
    __param(0, Arg('params', () => UserFavoriteSetParams)),
    __param(1, Ctx()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [UserFavoriteSetParams, Object]),
    __metadata("design:returntype", Promise)
], UserFavoriteResolver.prototype, "SetRecordFavoriteStatus", null);
UserFavoriteResolver = __decorate([
    Resolver(MJUserFavorite_)
], UserFavoriteResolver);
export { UserFavoriteResolver };
export default UserFavoriteResolver;
//# sourceMappingURL=UserFavoriteResolver.js.map