package com.liquidnet.client.admin.zhengzai.adam.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * 补签申请附件 URL 解析（支持逗号/分号/换行分隔或 JSON 字符串数组）。
 */
public final class CaomeiBadgeApplyProofUrls {

    private CaomeiBadgeApplyProofUrls() {
    }

    public static List<String> parse(String raw) {
        if (StringUtils.isBlank(raw)) {
            return Collections.emptyList();
        }
        String t = raw.trim();
        if (t.startsWith("[") && t.endsWith("]")) {
            try {
                JSONArray arr = JSON.parseArray(t);
                if (arr != null && !arr.isEmpty()) {
                    Set<String> seen = new LinkedHashSet<>();
                    for (int i = 0; i < arr.size(); i++) {
                        String u = StringUtils.trimToEmpty(arr.getString(i));
                        if (StringUtils.isNotBlank(u)) {
                            seen.add(u);
                        }
                    }
                    return new ArrayList<>(seen);
                }
            } catch (Exception ignored) {
                // fall through to delimiter split
            }
        }
        String[] parts = t.split("[,;\\n]+");
        List<String> out = new ArrayList<>();
        for (String p : parts) {
            String u = StringUtils.trimToEmpty(p);
            if (StringUtils.isNotBlank(u)) {
                out.add(u);
            }
        }
        return out;
    }
}
