/*
 * Decompiled with CFR 0.152.
 */
package com.hst.framework.util;

import java.util.HashSet;

public class HstSqlInUtil {
    private static final int MAX_LENGTH = 500;

    public static String getParameterArray(String[] parameters) {
        return HstSqlInUtil.getParameterArray(parameters, Integer.MAX_VALUE);
    }

    public static String getParameterArray(String values, String splitChar) {
        String[] parameters = values.split(splitChar);
        return HstSqlInUtil.getParameterArray(parameters, Integer.MAX_VALUE);
    }

    private static String getParameterArray(String[] parameters, int maxLenth) {
        if (parameters == null || parameters.length < 1) {
            return "('')";
        }
        StringBuffer pStringBuffer = new StringBuffer("(");
        parameters = HstSqlInUtil.filterSameElement(parameters);
        int i = 0;
        while (i < parameters.length) {
            if (i < maxLenth) {
                pStringBuffer = i == maxLenth - 1 || i == parameters.length - 1 ? pStringBuffer.append("'").append(parameters[i]).append("')") : pStringBuffer.append("'").append(parameters[i]).append("',");
            }
            ++i;
        }
        return pStringBuffer.toString();
    }

    public static String[][] splitQueryParameters(String[] queryParameters) {
        if (queryParameters == null || queryParameters.length == 0) {
            return new String[0][0];
        }
        queryParameters = HstSqlInUtil.filterSameElement(queryParameters);
        int m = queryParameters.length % 500;
        int n = queryParameters.length / 500;
        n = m != 0 ? n + 1 : n;
        String[][] results = new String[n][];
        int i = 0;
        while (i < n) {
            int size = 500;
            if (i == n - 1 && m != 0) {
                size = m;
            }
            results[i] = new String[size];
            System.arraycopy(queryParameters, 0 + i * 500, results[i], 0, size);
            ++i;
        }
        return results;
    }

    public static String[][] splitQueryParameters(String[] queryParameters, int subStringLength) {
        if (queryParameters == null || queryParameters.length == 0) {
            return new String[0][0];
        }
        queryParameters = HstSqlInUtil.filterSameElement(queryParameters);
        int m = queryParameters.length % subStringLength;
        int n = queryParameters.length / subStringLength;
        n = m != 0 ? n + 1 : n;
        String[][] results = new String[n][];
        int i = 0;
        while (i < n) {
            int size = subStringLength;
            if (i == n - 1 && m != 0) {
                size = m;
            }
            results[i] = new String[size];
            System.arraycopy(queryParameters, 0 + i * subStringLength, results[i], 0, size);
            ++i;
        }
        return results;
    }

    public static String[] filterSameElement(String[] queryParameters) {
        if (queryParameters == null || queryParameters.length < 1) {
            return new String[0];
        }
        HashSet<String> set = new HashSet<String>();
        int i = 0;
        while (i < queryParameters.length) {
            set.add(queryParameters[i]);
            ++i;
        }
        if (set.size() == queryParameters.length) {
            return queryParameters;
        }
        String[] results = new String[set.size()];
        set.toArray(results);
        return results;
    }

    public static String getParameterArray(String[] parameters, String attribute) {
        if (parameters == null || parameters.length < 1) {
            return String.valueOf(attribute) + " IN ('')";
        }
        String[][] parametersArray = HstSqlInUtil.splitQueryParameters(parameters);
        StringBuffer inWhereTemp = new StringBuffer("( ");
        inWhereTemp = inWhereTemp.append(attribute).append(" IN ");
        int i = 0;
        while (i < parametersArray.length) {
            inWhereTemp = parametersArray.length > 1 && i < parametersArray.length - 1 ? inWhereTemp.append(HstSqlInUtil.getParameterArray(parametersArray[i])).append(" OR ").append(attribute).append(" IN ") : inWhereTemp.append(HstSqlInUtil.getParameterArray(parametersArray[i]));
            ++i;
        }
        inWhereTemp = inWhereTemp.append(" )");
        return inWhereTemp.toString();
    }
}
