"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.XIMPLIFI_TENANT_ID = exports.DEMO_TEST_PARTNER_TENANT_ID = exports.VRP_TENANT_ID = void 0;
exports.whereTenantActive = whereTenantActive;
exports.assertTenantActive = assertTenantActive;
exports.mapTenantStatus = mapTenantStatus;
exports.whereTenantNotTestTeam = whereTenantNotTestTeam;
exports.whereTenantShouldBePaid = whereTenantShouldBePaid;
const utils_1 = require("@finalytic/utils");
const error_1 = require("../error");
// Tenants
exports.VRP_TENANT_ID = '7bfec41f-aa3f-4602-95d2-f7996e4f0e59';
exports.DEMO_TEST_PARTNER_TENANT_ID = '0b6a7bd6-56cc-46b5-b2ab-1bd7d1e994fa';
exports.XIMPLIFI_TENANT_ID = 'd6ab2bbe-e0fc-48d9-89a3-c3c05f4e79b5';
function whereTenantActive(where) {
    const statusWhere = {
        calculatedStatus: { _eq: 'active' },
        _or: [
            {
                calculatedBillingPartner: { calculatedStatus: { _eq: 'active' } },
            },
            {
                calculatedBillingPartnerId: { _is_null: true },
            },
        ],
    };
    if (where)
        return { _and: [statusWhere, where] };
    return statusWhere;
}
function assertTenantActive(tenant) {
    if (tenant.calculatedStatus !== 'active' ||
        tenant.calculatedBillingPartner?.calculatedStatus !== 'active')
        throw new error_1.HasuraError('Tenant or billing partner are not active');
}
function mapTenantStatus(tenant) {
    const status = tenant.calculatedStatus === 'active';
    const partnerStatus = tenant.calculatedBillingPartner?.calculatedStatus;
    return status && partnerStatus === 'active' ? 'active' : 'inactive';
}
function whereTenantNotTestTeam(where) {
    const statusWhere = {
        id: { _nin: [exports.VRP_TENANT_ID, exports.DEMO_TEST_PARTNER_TENANT_ID] },
        partnerId: {
            _neq: exports.DEMO_TEST_PARTNER_TENANT_ID,
        },
        _or: [
            { billingPartnerId: { _neq: exports.DEMO_TEST_PARTNER_TENANT_ID } },
            { billingPartnerId: { _is_null: true } },
        ],
    };
    if (where)
        return { _and: [statusWhere, where] };
    return statusWhere;
}
function whereTenantShouldBePaid(where, includePartnerTeams = false) {
    let statusWhere = {
        isOnboarding: {
            _eq: false, // has left onboarding
        },
        // tenant has no trial or trial ended
        _or: [
            {
                trialUntil: {
                    _lt: (0, utils_1.utc)().yyyymmdd(),
                },
            },
            {
                trialUntil: {
                    _is_null: true,
                },
            },
        ],
    };
    if (!includePartnerTeams)
        statusWhere = {
            ...statusWhere,
            calculatedIsBillable: {
                _eq: true, // no partner teams
            },
        };
    if (where)
        statusWhere = { _and: [statusWhere, where] };
    return whereTenantActive(statusWhere);
}
