/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the "Elastic License
 * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
 * Public License v 1"; you may not use this file except in compliance with, at
 * your election, the "Elastic License 2.0", the "GNU Affero General Public
 * License v3.0 only", or the "Server Side Public License, v 1".
 */

package org.elasticsearch.plugins;

import org.elasticsearch.index.mapper.Mapper;
import org.elasticsearch.index.mapper.MetadataFieldMapper;
import org.elasticsearch.index.mapper.RuntimeField;

import java.util.Collections;
import java.util.Map;
import java.util.function.Function;

/**
 * An extension point for {@link Plugin} implementations to add custom mappers
 */
public interface MapperPlugin {

    /**
     * Returns additional mapper implementations added by this plugin.
     * <p>
     * The key of the returned {@link Map} is the unique name for the mapper which will be used
     * as the mapping {@code type}, and the value is a {@link Mapper.TypeParser} to parse the
     * mapper settings into a {@link Mapper}.
     */
    default Map<String, Mapper.TypeParser> getMappers() {
        return Collections.emptyMap();
    }

    /**
     * Returns the runtime field implementations added by this plugin.
     * <p>
     * The key of the returned {@link Map} is the unique name for the field type which will be used
     * as the mapping {@code type}, and the value is a {@link RuntimeField.Parser} to parse the
     * field type settings into a {@link RuntimeField}.
     */
    default Map<String, RuntimeField.Parser> getRuntimeFields() {
        return Collections.emptyMap();
    }

    /**
     * Returns additional metadata mapper implementations added by this plugin.
     * <p>
     * The key of the returned {@link Map} is the unique name for the metadata mapper, which
     * is used in the mapping json to configure the metadata mapper, and the value is a
     * {@link MetadataFieldMapper.TypeParser} to parse the mapper settings into a
     * {@link MetadataFieldMapper}.
     */
    default Map<String, MetadataFieldMapper.TypeParser> getMetadataMappers() {
        return Collections.emptyMap();
    }

    /**
     * Returns a function that given an index name returns a predicate which fields must match in order to be returned by get mappings,
     * get index, get field mappings and field capabilities API. Useful to filter the fields that such API return. The predicate receives
     * the field name as input argument and should return true to show the field and false to hide it.
     */
    default Function<String, FieldPredicate> getFieldFilter() {
        return NOOP_FIELD_FILTER;
    }

    /**
     * The default field filter applied, which doesn't filter anything. That means that by default get mappings, get index
     * get field mappings and field capabilities API will return every field that's present in the mappings.
     */
    Function<String, FieldPredicate> NOOP_FIELD_FILTER = new Function<>() {
        @Override
        public FieldPredicate apply(String index) {
            return FieldPredicate.ACCEPT_ALL;
        }

        @Override
        public String toString() {
            return "accept all";
        }
    };
}
