package cn.axzo.workflow.core.engine.cmd.helper;

import lombok.extern.slf4j.Slf4j;
import org.flowable.form.model.FormField;
import org.flowable.form.model.LayoutDefinition;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.Objects;

/**
 * FormField Clone
 *
 * @author wangli
 * @since 2025-01-24 00:11
 */
@Slf4j
public class FormFieldClone {

    public static FormField clone(FormField formField) {
        FormField clone = new FormField();
        clone.setId(formField.getId());
        clone.setName(formField.getName());
        clone.setType(formField.getType());
        clone.setValue(formField.getValue());
        clone.setRequired(formField.isRequired());
        clone.setReadOnly(formField.isReadOnly());
        clone.setOverrideId(formField.isOverrideId());
        clone.setPlaceholder(formField.getPlaceholder());

        LayoutDefinition cloneLayout = new LayoutDefinition();
        if (Objects.nonNull(formField.getLayout())) {
            cloneLayout.setRow(formField.getLayout().getRow());
            clone.setLayout(cloneLayout);
        }

        if (!CollectionUtils.isEmpty(formField.getParams())) {
            clone.setParams(new HashMap<>(formField.getParams()));
        }
        return clone;
    }
}
