// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.microsoft.typespec.http.client.generator.mgmt.util;

import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.Client;
import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.Metadata;
import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.Parameter;
import com.microsoft.typespec.http.client.generator.core.extension.plugin.JavaSettings;
import com.microsoft.typespec.http.client.generator.core.util.SchemaUtil;
import io.github.encryptorcode.pluralize.Pluralize;
import java.util.Set;
import java.util.stream.Collectors;

public class Utils {

    public final static String METHOD_POSTFIX_WITH_RESPONSE = "WithResponse";

    public static String getDefaultName(Metadata m) {
        return SchemaUtil.getDefaultName(m);
    }

    public static String getJavaName(Metadata m) {
        return SchemaUtil.getJavaName(m);
    }

    public static boolean nonFlattenedParameter(Parameter p) {
        return !p.isFlattened();
    }

    public static String getNameForUngroupedOperations(Client client, FluentJavaSettings settings) {
        String nameForUngroupOperations = null;
        if (settings.getNameForUngroupedOperations().isPresent()) {
            nameForUngroupOperations = settings.getNameForUngroupedOperations().get();
        } else if (JavaSettings.getInstance().isFluentLite()) {
            nameForUngroupOperations = Constants.DEFAULT_NAME_FOR_UNGROUPED_OPERATIONS;

            Set<String> operationGroupNames
                = client.getOperationGroups().stream().map(Utils::getDefaultName).collect(Collectors.toSet());
            if (operationGroupNames.contains(nameForUngroupOperations)) {
                nameForUngroupOperations += Constants.OPERATION_GROUP_DEDUPLICATE_SUFFIX;
            }
        }
        return nameForUngroupOperations;
    }

    public static String getSingular(String name) {
        if (name == null) {
            return null;
        }

        return Pluralize.singular(name);
    }
}
