"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.pageSettings = void 0;
const typeorm_1 = require("typeorm");
const shell_1 = require("@things-factory/shell");
const biz_base_1 = require("@things-factory/biz-base");
const setting_base_1 = require("@things-factory/setting-base");
const code_base_1 = require("@things-factory/code-base");
exports.pageSettings = {
    async pageSettings(_, { codes, partners, settings, settingsDomain }, context) {
        const { domain, user, tx } = context.state;
        try {
            let promiseTask = [];
            if (codes) {
                promiseTask.push(queryCommonCode(codes, domain, tx, context));
            }
            if (partners) {
                promiseTask.push(queryPartners(partners, domain, tx, context));
            }
            if (settings) {
                promiseTask.push(querySettings(settings, settingsDomain, domain, tx));
            }
            return await Promise.all(promiseTask);
        }
        catch (error) {
            throw error;
        }
    }
};
async function queryCommonCode(codes, domain, tx, context) {
    const codeQb = tx.getRepository(code_base_1.CommonCode).createQueryBuilder('cc');
    (0, shell_1.buildQuery)(codeQb, { filters: codes }, context);
    codes = (await codeQb
        .leftJoinAndSelect('cc.details', 'ccdet')
        .orderBy('cc.name')
        .addOrderBy('ccdet.rank')
        .addOrderBy('ccdet.name')
        .getMany()).map((cc) => {
        return {
            id: cc.id,
            name: cc.name,
            description: cc.description,
            value: null,
            settingDetails: cc.details.map(det => {
                return {
                    id: det.id,
                    name: det.name,
                    description: det.description,
                    rank: det.rank
                };
            })
        };
    });
    return {
        type: 'code',
        details: codes
    };
}
async function queryPartners(partners, domain, tx, context) {
    var _a;
    let partnersBizplaceIds = [];
    let bizplaceIndex = (_a = partners.filters) === null || _a === void 0 ? void 0 : _a.findIndex(x => x.name === 'bizplace');
    if (bizplaceIndex && bizplaceIndex > -1) {
        partnersBizplaceIds = partners.filters
            .filter(x => x.name === 'bizplace')
            .map(item => {
            return item.value;
        });
        partners.filters.splice(bizplaceIndex, 1);
    }
    const convertedParams = (0, shell_1.convertListParams)({ filters: partners });
    if (!(partnersBizplaceIds.length > 0)) {
        const partnersBizplaces = await (0, biz_base_1.getPartnersBizplaces)(context.state.domain, context.state.user);
        partnersBizplaceIds = partnersBizplaces.map((bizplace) => bizplace.id || null);
        convertedParams.where.id = (0, typeorm_1.In)(partnersBizplaceIds);
    }
    else {
        convertedParams.where.id = (0, typeorm_1.In)(partnersBizplaceIds);
    }
    partners = (await tx.getRepository(biz_base_1.Bizplace).find(Object.assign(Object.assign({}, convertedParams), { relations: ['domain', 'company', 'users', 'creator', 'updater'] }))).map((biz) => {
        return {
            id: biz.id,
            name: biz.name,
            description: biz.description,
            value: null
        };
    });
    return {
        type: 'partner',
        details: partners
    };
}
async function querySettings(settingsFilter, partnerDomainId, domain, tx) {
    let settingsName = settingsFilter.map(setting => setting.value).flat();
    let domainSettings = (await tx
        .getRepository(setting_base_1.Setting)
        .createQueryBuilder('setting')
        .where('setting.name IN (:...names)', { names: settingsName })
        .andWhere('setting.domain = :domainId', { domainId: domain.id })
        .getMany()).map((setting) => {
        return {
            id: setting.id,
            name: setting.name,
            description: setting.description,
            value: setting.value
        };
    });
    let partnerSettings = [];
    if (partnerDomainId) {
        partnerSettings = (await tx
            .getRepository(setting_base_1.PartnerSetting)
            .createQueryBuilder('ps')
            .leftJoinAndSelect('ps.setting', 'setting')
            .where('ps.setting IN (:...settings)', { settings: domainSettings.map(setting => setting.id) })
            .andWhere('ps.domain = :domainId', { domainId: domain.id })
            .andWhere('ps.partnerDomain = :partnerDomainId', { partnerDomainId })
            .getMany()).map((ps) => {
            return {
                id: ps.setting.id,
                name: ps.setting.name,
                description: ps.setting.description,
                value: ps.value
            };
        });
    }
    //filter out domain settings that exist in partner settings
    const partnerSettingIds = new Set(partnerSettings.map(ps => ps.id));
    const filteredDomainSettings = domainSettings.filter(ds => !partnerSettingIds.has(ds.id));
    let settings = [...filteredDomainSettings, ...partnerSettings];
    return {
        type: 'setting',
        details: settings
    };
}
//# sourceMappingURL=page-settings.js.map