package tech.easyflow.ai.node;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.easyagents.flow.core.node.BaseNode;
import com.easyagents.flow.core.parser.BaseNodeParser;
import tech.easyflow.ai.node.filegeneration.FileGenerationRules;
import tech.easyflow.ai.node.filegeneration.SourceFormat;
import tech.easyflow.ai.node.filegeneration.TargetFormat;

public class MakeFileNodeParser extends BaseNodeParser {

    @Override
    public BaseNode doParse(JSONObject root, JSONObject data, JSONObject tinyflow) {
        String targetFormatValue = data == null ? null : data.getString("targetFormat");
        if (StrUtil.isBlank(targetFormatValue)) {
            targetFormatValue = TargetFormat.DOCX.getValue();
        }
        String sourceFormatValue = data == null ? null : data.getString("sourceFormat");
        if (StrUtil.isBlank(sourceFormatValue)) {
            sourceFormatValue = SourceFormat.MARKDOWN.getValue();
        }
        String fileName = data == null ? null : StrUtil.trimToNull(data.getString("fileName"));
        String templateStyle = FileGenerationRules.normalizeTemplateStyle(data == null ? null : data.getString("templateStyle"));
        TargetFormat targetFormat = TargetFormat.fromValue(targetFormatValue);
        SourceFormat sourceFormat = SourceFormat.fromValue(sourceFormatValue);
        FileGenerationRules.requireSupportedCombination(sourceFormat, targetFormat);
        return new MakeFileNode(targetFormat.getValue(), sourceFormat.getValue(), fileName, templateStyle);
    }

    public String getNodeName() {
        return "make-file";
    }
}
