package cn.axzo.workflow.core.engine.event;

import org.apache.commons.lang3.StringUtils;
import org.flowable.common.engine.api.FlowableIllegalArgumentException;
import org.flowable.common.engine.api.delegate.event.FlowableEventType;

import java.util.ArrayList;
import java.util.List;

/**
 * 异步任务异常事件类型
 *
 * @author wangli
 * @since 2024/4/17 16:47
 */
public enum ErrorInfoEventType implements FlowableEventType {

    NEW_ERROR;

    public static final ErrorInfoEventType[] EMPTY_ARRAY = new ErrorInfoEventType[]{};

    /**
     * @param string the string containing a comma-separated list of event-type names
     * @return a list of FlowableEngineEventType based on the given list.
     * @throws FlowableIllegalArgumentException when one of the given string is not a valid type name
     */
    public static ErrorInfoEventType[] getTypesFromString(String string) {
        List<ErrorInfoEventType> result = new ArrayList<>();
        if (string != null && !string.isEmpty()) {
            String[] split = StringUtils.split(string, ",");
            for (String typeName : split) {
                boolean found = false;
                for (ErrorInfoEventType type : values()) {
                    if (typeName.toUpperCase().equals(type.name())) {
                        result.add(type);
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    throw new FlowableIllegalArgumentException("Invalid event-type: " + typeName);
                }
            }
        }

        return result.toArray(EMPTY_ARRAY);
    }
}
