"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.whereGlListingIsInactive = void 0;
exports.whereListingIsActive = whereListingIsActive;
exports.whereGlListingIsActive = whereGlListingIsActive;
const utils_1 = require("@finalytic/utils");
function whereListingIsActive() {
    return {
        _or: [
            {
                // LEGACY teams
                tenant: {
                    isGeneralLedger: { _eq: false },
                },
                calculatedStatus: { _eq: 'active' },
            },
            {
                // GL teams
                tenant: {
                    isGeneralLedger: { _eq: true },
                },
                ...whereGlListingIsActive(undefined, (0, utils_1.utc)().yyyymmdd()),
            },
        ],
    };
}
function whereGlListingIsActive(where, date = (0, utils_1.utc)().yyyymmdd(), includeUpcoming = false) {
    const statusWhere = {
        _or: [
            {
                _not: {
                    ownershipPeriods: {},
                },
            },
            {
                _not: (0, exports.whereGlListingIsInactive)(undefined, date, includeUpcoming),
            },
        ],
    };
    if (where)
        return { _and: [statusWhere, where] };
    return statusWhere;
}
const whereGlListingIsInactive = (where, date = (0, utils_1.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(utils_1.hasValue),
        },
    };
    if (where)
        return { _and: [statusWhere, where] };
    return statusWhere;
};
exports.whereGlListingIsInactive = whereGlListingIsInactive;
