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, EntityPermissionType, RunView } from '@memberjunction/core';
import { Arg, Ctx, DeleteOptionsInput, Mutation } from '@memberjunction/server';
import { MJFileCategoryResolver as FileCategoryResolverBase, MJFileCategory_ } from '../generated/generated.js';
import { GetReadWriteProvider } from '../util.js';
export class FileResolver extends FileCategoryResolverBase {
    async DeleteFileCategory(ID, options, { providers, userPayload }) {
        const key = new CompositeKey();
        key.LoadFromSingleKeyValuePair('ID', ID);
        const p = GetReadWriteProvider(providers);
        if (!(await this.BeforeDelete(p, key))) {
            return null;
        }
        const user = this.GetUserFromPayload(userPayload);
        const fileEntity = await p.GetEntityObject('Files', user);
        const fileCategoryEntity = await p.GetEntityObject('File Categories', user);
        fileEntity.CheckPermissions(EntityPermissionType.Update, true);
        fileCategoryEntity.CheckPermissions(EntityPermissionType.Delete, true);
        await fileCategoryEntity.Load(ID);
        const returnValue = fileCategoryEntity.GetAll();
        await p.BeginTransaction();
        try {
            const rv = new RunView();
            const filesResult = await rv.RunView({
                EntityName: 'Files',
                ExtraFilter: `CategoryID='${fileCategoryEntity.ID}'`,
                ResultType: 'entity_object',
            }, user);
            if (filesResult) {
                for (const file of filesResult.Results) {
                    const fileEntity = await p.GetEntityObject('Files', user);
                    await fileEntity.Load(file.ID);
                    fileEntity.CategoryID = fileCategoryEntity.ParentID;
                    await fileEntity.Save();
                }
            }
            await fileCategoryEntity.Delete(options);
            await p.CommitTransaction();
        }
        catch (error) {
            await p.RollbackTransaction();
            throw error;
        }
        await this.AfterDelete(p, key);
        return returnValue;
    }
}
__decorate([
    Mutation(() => MJFileCategory_),
    __param(0, Arg('ID', () => String)),
    __param(1, Arg('options___', () => DeleteOptionsInput)),
    __param(2, Ctx()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, DeleteOptionsInput, Object]),
    __metadata("design:returntype", Promise)
], FileResolver.prototype, "DeleteFileCategory", null);
//# sourceMappingURL=FileCategoryResolver.js.map