import { utc } from '@finalytic/utils';
import { HasuraError } from '../error';
// Tenants
export const VRP_TENANT_ID = '7bfec41f-aa3f-4602-95d2-f7996e4f0e59';
export const DEMO_TEST_PARTNER_TENANT_ID = '0b6a7bd6-56cc-46b5-b2ab-1bd7d1e994fa';
export const XIMPLIFI_TENANT_ID = 'd6ab2bbe-e0fc-48d9-89a3-c3c05f4e79b5';
export 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;
}
export function assertTenantActive(tenant) {
    if (tenant.calculatedStatus !== 'active' ||
        tenant.calculatedBillingPartner?.calculatedStatus !== 'active')
        throw new HasuraError('Tenant or billing partner are not active');
}
export function mapTenantStatus(tenant) {
    const status = tenant.calculatedStatus === 'active';
    const partnerStatus = tenant.calculatedBillingPartner?.calculatedStatus;
    return status && partnerStatus === 'active' ? 'active' : 'inactive';
}
export function whereTenantNotTestTeam(where) {
    const statusWhere = {
        id: { _nin: [VRP_TENANT_ID, DEMO_TEST_PARTNER_TENANT_ID] },
        partnerId: {
            _neq: DEMO_TEST_PARTNER_TENANT_ID,
        },
        _or: [
            { billingPartnerId: { _neq: DEMO_TEST_PARTNER_TENANT_ID } },
            { billingPartnerId: { _is_null: true } },
        ],
    };
    if (where)
        return { _and: [statusWhere, where] };
    return statusWhere;
}
export function whereTenantShouldBePaid(where, includePartnerTeams = false) {
    let statusWhere = {
        isOnboarding: {
            _eq: false, // has left onboarding
        },
        // tenant has no trial or trial ended
        _or: [
            {
                trialUntil: {
                    _lt: 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);
}
