// Copyright (c) 2009-2026 SAP SE, All Rights Reserved
/**
 * @fileOverview Utility for transforming ContentFinder event parameters to the GraphQL API format.
 * @version 1.148.1
 */
sap.ui.define([], () => {
    "use strict";

    /**
     * Transforms the filter parameters received from the ContentFinder's
     * <code>visualizationFilterApplied</code> event to the format required by the GraphQL content API.
     *
     * @param {object} oParams The ContentFinder filter parameters.
     * @param {object} [oParams.pagination] Pagination parameters.
     * @param {int} [oParams.pagination.top] Number of items to fetch.
     * @param {int} [oParams.pagination.skip] Number of items to skip.
     * @param {string} [oParams.search] Search term.
     * @param {string[]} [oParams.types] Visualization type filters.
     * @returns {{top: int, skip: ?int, filter: object[]}} The filter parameters in GraphQL API format.
     *
     * @since 1.148.0
     * @private
     */
    function transformFilterParams (oParams) {
        const iTop = oParams?.pagination?.top;
        const iSkip = oParams?.pagination?.skip;
        const sSearchTerm = oParams?.search;
        const aTypes = oParams?.types || [];
        const aExpandedTypes = aTypes.length > 0 ? [{ in: aTypes }] : [];
        const oSearchFilter = sSearchTerm
            ? {
                type: aExpandedTypes,
                descriptor: [
                    {
                        conditions: [{
                            propertyPath: "/sap.app/title",
                            stringFilter: [{ containsIgnoreCase: sSearchTerm }]
                        }]
                    },
                    {
                        conditions: [{
                            propertyPath: "/sap.app/subTitle",
                            stringFilter: [{ containsIgnoreCase: sSearchTerm }]
                        }]
                    },
                    {
                        conditions: [{
                            propertyPath: "/sap.app/info",
                            stringFilter: [{ containsIgnoreCase: sSearchTerm }]
                        }]
                    },
                    {
                        conditions: [{
                            propertyPath: "/sap.fiori/registrationIds",
                            anyFilter: [{
                                conditions: [{
                                    stringFilter: [{ containsIgnoreCase: sSearchTerm }]
                                }]
                            }]
                        }]
                    }
                ]
            }
            : { type: aExpandedTypes };

        return { top: iTop, skip: iSkip, filter: [oSearchFilter] };
    }

    return { transformFilterParams };
});
