export var KnownCaseFields;
(function (KnownCaseFields) {
    KnownCaseFields["id"] = "id";
    KnownCaseFields["subject"] = "subject";
    KnownCaseFields["description"] = "description";
    KnownCaseFields["category"] = "category";
    KnownCaseFields["productId"] = "productId";
})(KnownCaseFields || (KnownCaseFields = {}));
const isCustomFieldName = (fieldName) => !Object.values(KnownCaseFields).includes(fieldName);
export const caseAssistCaseSelector = (state) => {
    const selectFieldValue = (fieldName) => caseAssistCaseInputValueSelector(state, fieldName) ??
        caseAssistCaseFieldValueSelector(state, fieldName);
    return {
        id: selectFieldValue(KnownCaseFields.id),
        subject: selectFieldValue(KnownCaseFields.subject),
        description: selectFieldValue(KnownCaseFields.description),
        category: selectFieldValue(KnownCaseFields.category),
        productId: selectFieldValue(KnownCaseFields.productId),
        custom: {
            ...caseAssistCustomCaseInputValuesSelector(state),
            ...caseAssistCustomCaseFieldValuesSelector(state),
        },
    };
};
export const caseAssistCaseInputValueSelector = (state, fieldName) => state?.caseInput?.[fieldName]?.value;
export const caseAssistCustomCaseInputValuesSelector = (state) => Object.keys(state?.caseInput ?? []).reduce((customFields, fieldName) => {
    if (isCustomFieldName(fieldName)) {
        const value = state?.caseInput?.[fieldName]?.value;
        if (value) {
            // oxlint-disable-next-line oxc/no-accumulating-spread -- <>
            return { ...customFields, [fieldName]: value };
        }
    }
    return customFields;
}, {});
export const caseAssistCaseFieldValueSelector = (state, fieldName) => state?.caseField?.fields[fieldName]?.value;
export const caseAssistCustomCaseFieldValuesSelector = (state) => Object.keys(state?.caseField?.fields ?? []).reduce((customFields, fieldName) => {
    if (isCustomFieldName(fieldName)) {
        const value = state?.caseField?.fields?.[fieldName]?.value;
        if (value) {
            // oxlint-disable-next-line oxc/no-accumulating-spread -- <>
            return { ...customFields, [fieldName]: value };
        }
    }
    return customFields;
}, {});
export const caseAssistCaseClassificationSelector = (state, classificationId, autoSelection = false) => {
    const classificationFieldName = Object.keys(state?.caseField?.fields ?? {}).find((fieldName) => state?.caseField?.fields[fieldName].suggestions.some((suggestion) => suggestion.id === classificationId));
    if (!classificationFieldName) {
        throw new Error(`Classification with ID '${classificationId}' could not be found.`);
    }
    const classificationField = state?.caseField?.fields[classificationFieldName];
    const classification = classificationField?.suggestions.find((suggestion) => suggestion.id === classificationId);
    const result = {
        classificationId: classification?.id ?? '',
        responseId: state.caseField?.status.lastResponseId ?? '',
        fieldName: classificationFieldName,
        classification: {
            value: classification?.value ?? '',
            confidence: classification?.confidence ?? 0,
        },
    };
    if (autoSelection) {
        return { ...result, autoSelection };
    }
    return result;
};
export const caseAssistDocumentSuggestionSelector = (state, suggestionId, fromQuickview = false, openDocument = false) => {
    let suggestionIdx;
    const suggestion = state.documentSuggestion?.documents.find((s, idx) => {
        const isFound = s.uniqueId === suggestionId;
        if (isFound) {
            suggestionIdx = idx + 1;
        }
        return isFound;
    });
    if (!suggestion) {
        throw new Error(`Document Suggestion with ID '${suggestionId}' could not be found.`);
    }
    const result = {
        suggestionId: suggestion.uniqueId,
        permanentId: suggestion.fields.permanentid,
        responseId: state.documentSuggestion?.status.lastResponseId ?? '',
        suggestion: {
            documentUri: suggestion.fields.uri,
            documentUriHash: suggestion.fields.urihash,
            documentTitle: suggestion.title,
            documentUrl: suggestion.clickUri,
            documentPosition: suggestionIdx ?? 0,
            sourceName: suggestion.fields.source,
        },
    };
    if (fromQuickview) {
        return { ...result, fromQuickview };
    }
    if (openDocument) {
        return { ...result, openDocument };
    }
    return result;
};
