"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FeatureSearchService = void 0;
class FeatureSearchService {
    constructor({ featureSearchStore }, { getLogger }) {
        this.parseOperatorValue = (field, value) => {
            const pattern = /^(IS|IS_NOT|IS_ANY_OF|IS_NONE_OF|INCLUDE|DO_NOT_INCLUDE|INCLUDE_ALL_OF|INCLUDE_ANY_OF|EXCLUDE_IF_ANY_OF|EXCLUDE_ALL|IS_BEFORE|IS_ON_OR_AFTER):(.+)$/;
            const match = value.match(pattern);
            if (match) {
                return {
                    field,
                    operator: match[1],
                    values: match[2].split(','),
                };
            }
            return null;
        };
        this.convertToQueryParams = (params) => {
            const queryParams = [];
            if (params.state) {
                const parsedState = this.parseOperatorValue('stale', params.state);
                if (parsedState) {
                    parsedState.values = parsedState.values.map((value) => value === 'active' ? 'false' : 'true');
                    queryParams.push(parsedState);
                }
            }
            if (params.createdAt) {
                const parsed = this.parseOperatorValue('features.created_at', params.createdAt);
                if (parsed)
                    queryParams.push(parsed);
            }
            ['tag', 'segment', 'project'].forEach((field) => {
                if (params[field]) {
                    const parsed = this.parseOperatorValue(field, params[field]);
                    if (parsed)
                        queryParams.push(parsed);
                }
            });
            return queryParams;
        };
        this.featureSearchStore = featureSearchStore;
        this.logger = getLogger('services/feature-search-service.ts');
    }
    async search(params) {
        const queryParams = this.convertToQueryParams(params);
        const { features, total } = await this.featureSearchStore.searchFeatures({
            ...params,
            limit: params.limit,
        }, queryParams);
        return {
            features,
            total,
        };
    }
}
exports.FeatureSearchService = FeatureSearchService;
//# sourceMappingURL=feature-search-service.js.map