"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DesignPackageMutation = void 0;
const tslib_1 = require("tslib");
const type_graphql_1 = require("type-graphql");
const shell_1 = require("@things-factory/shell");
const design_package_1 = require("./design-package");
const process_1 = require("../process/process");
const design_package_type_1 = require("./design-package-type");
const thumbnail_1 = require("../../controllers/thumbnail");
let DesignPackageMutation = class DesignPackageMutation {
    async createDesignPackage(designPackage, context) {
        const { domain, user, tx } = context.state;
        const repository = tx.getRepository(design_package_1.DesignPackage);
        const oldDesignPackage = await repository.findOneBy({
            name: designPackage.name,
            domain: { id: domain.id }
        });
        if (oldDesignPackage) {
            throw new Error(context.t('error.design-package name is already taken', { name: designPackage.name }));
        }
        const newDesignPackage = Object.assign({}, designPackage);
        const base64 = await (0, thumbnail_1.thumbnail)({
            model: designPackage.model,
            context
        });
        if (base64) {
            newDesignPackage.thumbnail = 'data:image/png;base64,' + base64.toString('base64');
        }
        else {
            newDesignPackage.thumbnail = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
        }
        return await repository.save(Object.assign(Object.assign({ domain }, newDesignPackage), { creator: user, updater: user }));
    }
    async updateDesignPackage(id, patch, context) {
        const { domain, user, tx } = context.state;
        const repository = tx.getRepository(design_package_1.DesignPackage);
        const designPackage = await repository.findOne({
            where: { domain: { id: domain.id }, id }
        });
        if (patch.model) {
            const base64 = await (0, thumbnail_1.thumbnail)({
                model: patch.model,
                context
            });
            if (base64) {
                patch.thumbnail = 'data:image/png;base64,' + base64.toString('base64');
            }
            else {
                patch.thumbnail = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
            }
        }
        return await repository.save(Object.assign(Object.assign(Object.assign({}, designPackage), patch), { updater: user }));
    }
    async deleteDesignPackage(id, context) {
        const { domain, tx } = context.state;
        await tx.getRepository(design_package_1.DesignPackage).delete({ domain: { id: domain.id }, id });
        return true;
    }
    async registerProcessAsTemplate(id, name, description, visibility, context) {
        const { domain, user, notify, tx } = context.state;
        const boardTemplateRepository = tx.getRepository(design_package_1.DesignPackage);
        const process = await tx.getRepository(process_1.Process).findOne({
            where: { domain: { id: domain.id }, id }
        });
        if (!process) {
            throw `Process given id(${id}) is not found`;
        }
        name || (name = process.name);
        const { model, thumbnail } = process;
        const designPackage = await boardTemplateRepository.findOne({
            where: { domain: { id: domain.id }, name }
        });
        const registered = designPackage
            ? await boardTemplateRepository.save(Object.assign(Object.assign({}, designPackage), { name, description: description || designPackage.description, visibility: visibility || designPackage.visibility, model,
                thumbnail, updater: user }))
            : await boardTemplateRepository.save({
                domain: { id: domain.id },
                name,
                description: description || process.description,
                model,
                thumbnail,
                visibility: visibility || 'private',
                updater: user,
                creator: user
            });
        notify &&
            notify({
                mode: 'in-app',
                title: `DesignPackage '${registered.name}' registered`,
                body: `DesignPackage '${registered.name}' registered by ${user.name}\n${registered.description}`,
                image: (0, shell_1.getRedirectSubdomainPath)(context, domain.subdomain, `/design-package-thumbnail/${registered.id}`),
                url: (0, shell_1.getRedirectSubdomainPath)(context, domain.subdomain, `/design-package-viewer/${registered.id}`)
            });
        return registered;
    }
};
exports.DesignPackageMutation = DesignPackageMutation;
tslib_1.__decorate([
    (0, type_graphql_1.Directive)('@transaction'),
    (0, type_graphql_1.Directive)('@privilege(category: "design-package", privilege: "mutation", domainOwnerGranted: true)'),
    (0, type_graphql_1.Mutation)(returns => design_package_1.DesignPackage, { description: 'To create new DesignPackage' }),
    tslib_1.__param(0, (0, type_graphql_1.Arg)('designPackage')),
    tslib_1.__param(1, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [design_package_type_1.NewDesignPackage, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], DesignPackageMutation.prototype, "createDesignPackage", null);
tslib_1.__decorate([
    (0, type_graphql_1.Directive)('@transaction'),
    (0, type_graphql_1.Directive)('@privilege(category: "design-package", privilege: "mutation", domainOwnerGranted: true)'),
    (0, type_graphql_1.Mutation)(returns => design_package_1.DesignPackage, { description: 'To modify DesignPackage information' }),
    tslib_1.__param(0, (0, type_graphql_1.Arg)('id')),
    tslib_1.__param(1, (0, type_graphql_1.Arg)('patch')),
    tslib_1.__param(2, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String, design_package_type_1.DesignPackagePatch, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], DesignPackageMutation.prototype, "updateDesignPackage", null);
tslib_1.__decorate([
    (0, type_graphql_1.Directive)('@transaction'),
    (0, type_graphql_1.Directive)('@privilege(category: "design-package", privilege: "mutation", domainOwnerGranted: true)'),
    (0, type_graphql_1.Mutation)(returns => Boolean, { description: 'To delete DesignPackage' }),
    tslib_1.__param(0, (0, type_graphql_1.Arg)('id')),
    tslib_1.__param(1, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], DesignPackageMutation.prototype, "deleteDesignPackage", null);
tslib_1.__decorate([
    (0, type_graphql_1.Directive)('@transaction'),
    (0, type_graphql_1.Directive)('@privilege(category: "design-package", privilege: "mutation", domainOwnerGranted: true)'),
    (0, type_graphql_1.Mutation)(returns => design_package_1.DesignPackage, { description: 'To register a process as a process template with the given ID' }),
    tslib_1.__param(0, (0, type_graphql_1.Arg)('id', { description: 'process Id to be regiestered' })),
    tslib_1.__param(1, (0, type_graphql_1.Arg)('name', { description: 'name of process template to be regiestered' })),
    tslib_1.__param(2, (0, type_graphql_1.Arg)('description', { description: 'description of process template to be regiestered' })),
    tslib_1.__param(3, (0, type_graphql_1.Arg)('visibility', { description: 'visibility of process template to be regiestered' })),
    tslib_1.__param(4, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String, String, String, String, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], DesignPackageMutation.prototype, "registerProcessAsTemplate", null);
exports.DesignPackageMutation = DesignPackageMutation = tslib_1.__decorate([
    (0, type_graphql_1.Resolver)(design_package_1.DesignPackage)
], DesignPackageMutation);
//# sourceMappingURL=design-package-mutation.js.map