export const TestableGroupTypeId = {
    AND: 'AND',
    OR: 'OR',
    OR_EXCLUSIVE: 'OR_EXCLUSIVE',
    LEGACY_ANDOR: 'ANDOR',
    CONTEXT: 'context',
} as const

export type TestableGroupMode =
    | typeof TestableGroupTypeId.AND
    | typeof TestableGroupTypeId.OR
    | typeof TestableGroupTypeId.OR_EXCLUSIVE

export type LegacyTestableGroupMode =
    | TestableGroupMode
    | typeof TestableGroupTypeId.LEGACY_ANDOR

export type TestableCompositeGroupType =
    | LegacyTestableGroupMode
    | typeof TestableGroupTypeId.CONTEXT

export const selectableTestableGroupModes: readonly TestableGroupMode[] = [
    TestableGroupTypeId.AND,
    TestableGroupTypeId.OR,
    TestableGroupTypeId.OR_EXCLUSIVE,
]

export const nonContextTestableGroupTypes: readonly LegacyTestableGroupMode[] = [
    ...selectableTestableGroupModes,
    TestableGroupTypeId.LEGACY_ANDOR,
]

export const testableCompositeGroupTypes: readonly TestableCompositeGroupType[] = [
    ...nonContextTestableGroupTypes,
    TestableGroupTypeId.CONTEXT,
]

export const isTestableGroupMode = (type?: string): type is TestableGroupMode => {
    return selectableTestableGroupModes.includes(type as TestableGroupMode)
}

export const isNonContextTestableGroupType = (type?: string): type is LegacyTestableGroupMode => {
    return nonContextTestableGroupTypes.includes(type as LegacyTestableGroupMode)
}

export const isTestableCompositeGroupType = (type?: string): type is TestableCompositeGroupType => {
    return testableCompositeGroupTypes.includes(type as TestableCompositeGroupType)
}

export const normalizeTestableGroupMode = (type?: string): TestableGroupMode | undefined => {
    if (type === TestableGroupTypeId.LEGACY_ANDOR) {
        return TestableGroupTypeId.OR
    }

    if (isTestableGroupMode(type)) {
        return type
    }
}
