import { hasValue, utc } from '@vrplatform/utils';
export function whereListingIsActive() {
    return {
        _or: [
            {
                // LEGACY teams
                tenant: {
                    isGeneralLedger: { _eq: false },
                },
                calculatedStatus: { _eq: 'active' },
            },
            {
                // GL teams
                tenant: {
                    isGeneralLedger: { _eq: true },
                },
                ...whereGlListingIsActive(undefined, utc().yyyymmdd()),
            },
        ],
    };
}
export function whereGlListingIsActive(where, date = utc().yyyymmdd(), includeUpcoming = false) {
    const statusWhere = {
        _or: [
            {
                _not: {
                    ownershipPeriods: {},
                },
            },
            {
                _not: whereGlListingIsInactive(undefined, date, includeUpcoming),
            },
        ],
    };
    if (where)
        return { _and: [statusWhere, where] };
    return statusWhere;
}
export const whereGlListingIsInactive = (where, date = utc().yyyymmdd(), includeUpcoming = false) => {
    const statusWhere = {
        ownershipPeriods: {
            setListingInactive: { _eq: true },
            _or: [
                includeUpcoming
                    ? {
                        startAt: {
                            _gte: date,
                        },
                    }
                    : undefined,
                {
                    startAt: {
                        _lt: date,
                    },
                    endAt: {
                        _gte: date,
                    },
                },
                {
                    startAt: {
                        _lt: date,
                    },
                    endAt: {
                        _is_null: true,
                    },
                },
            ].filter(hasValue),
        },
    };
    if (where)
        return { _and: [statusWhere, where] };
    return statusWhere;
};
