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, Field, ObjectType, Query } from "type-graphql";
import { DataContext } from "@memberjunction/data-context";
import { GetReadOnlyDataSource, GetReadOnlyProvider } from "../util.js";
import { Metadata } from "@memberjunction/core";
let GetDataContextItemDataOutputType = class GetDataContextItemDataOutputType {
    Success;
    ErrorMessage;
    Result;
};
__decorate([
    Field(() => Boolean),
    __metadata("design:type", Boolean)
], GetDataContextItemDataOutputType.prototype, "Success", void 0);
__decorate([
    Field(() => String, { nullable: true }),
    __metadata("design:type", String)
], GetDataContextItemDataOutputType.prototype, "ErrorMessage", void 0);
__decorate([
    Field(() => String, { nullable: true }),
    __metadata("design:type", String)
], GetDataContextItemDataOutputType.prototype, "Result", void 0);
GetDataContextItemDataOutputType = __decorate([
    ObjectType()
], GetDataContextItemDataOutputType);
export { GetDataContextItemDataOutputType };
let GetDataContextDataOutputType = class GetDataContextDataOutputType {
    Success;
    ErrorMessages;
    Results;
};
__decorate([
    Field(() => Boolean),
    __metadata("design:type", Boolean)
], GetDataContextDataOutputType.prototype, "Success", void 0);
__decorate([
    Field(() => [String], { nullable: 'itemsAndList' }),
    __metadata("design:type", Array)
], GetDataContextDataOutputType.prototype, "ErrorMessages", void 0);
__decorate([
    Field(() => [String], { nullable: 'itemsAndList' }),
    __metadata("design:type", Array)
], GetDataContextDataOutputType.prototype, "Results", void 0);
GetDataContextDataOutputType = __decorate([
    ObjectType()
], GetDataContextDataOutputType);
export { GetDataContextDataOutputType };
export class GetDataContextDataResolver {
    async GetDataContextItemData(DataContextItemID, appCtx) {
        try {
            const ds = GetReadOnlyDataSource(appCtx.dataSources, {
                allowFallbackToReadWrite: true,
            });
            const md = GetReadOnlyProvider(appCtx.providers, { allowFallbackToReadWrite: true });
            const dciData = await md.GetEntityObject("Data Context Items", appCtx.userPayload.userRecord);
            if (await dciData.Load(DataContextItemID)) {
                const dci = DataContext.CreateDataContextItem();
                await dci.LoadMetadataFromEntityRecord(dciData, Metadata.Provider, appCtx.userPayload.userRecord);
                if (await dci.LoadData(ds)) {
                    return {
                        Success: true,
                        ErrorMessage: null,
                        Result: JSON.stringify(dci.Data),
                    };
                }
                else {
                    return {
                        Success: false,
                        ErrorMessage: 'Error loading data context item data',
                        Result: null,
                    };
                }
            }
            else {
                return {
                    Success: false,
                    ErrorMessage: 'Error loading data context item metadata',
                    Result: null,
                };
            }
        }
        catch (e) {
            return {
                Success: false,
                ErrorMessage: e,
                Result: null,
            };
        }
    }
    async GetDataContextData(DataContextID, appCtx) {
        try {
            const dc = new DataContext();
            const ds = GetReadOnlyDataSource(appCtx.dataSources, {
                allowFallbackToReadWrite: true,
            });
            const success = await dc.Load(DataContextID, ds, true, false, 0, appCtx.userPayload.userRecord);
            if (success) {
                const retVal = {
                    Success: true,
                    ErrorMessages: null,
                    Results: dc.Items.map((item) => {
                        return JSON.stringify(item.Data);
                    }),
                };
                return retVal;
            }
            else {
                return {
                    Success: false,
                    ErrorMessages: ['Error loading data context'],
                    Results: null,
                };
            }
        }
        catch (e) {
            return {
                Success: false,
                ErrorMessages: [e],
                Results: null,
            };
        }
    }
}
__decorate([
    Query(() => GetDataContextItemDataOutputType),
    __param(0, Arg('DataContextItemID', () => String)),
    __param(1, Ctx()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object]),
    __metadata("design:returntype", Promise)
], GetDataContextDataResolver.prototype, "GetDataContextItemData", null);
__decorate([
    Query(() => GetDataContextDataOutputType),
    __param(0, Arg('DataContextID', () => String)),
    __param(1, Ctx()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object]),
    __metadata("design:returntype", Promise)
], GetDataContextDataResolver.prototype, "GetDataContextData", null);
//# sourceMappingURL=GetDataContextDataResolver.js.map