package main

import (
	"github.com/manifoldco/promptui"
)

type opType int32

const (
	Add       opType = 1
	OverWrite opType = 2
)

var opIndex = map[string]opType{
	"新增":          Add,
	"覆盖(有则改，无则增)": OverWrite,
}

type Set struct {
	Op                  opType
	OverWriteJudColumns []string
}

func setOp(title []string) *Set {
	var set = new(Set)
	for {
		prompt := promptui.Select{
			Label: "请选择需要执行的操作: ",
			Items: []string{"新增"}, //, "覆盖(有则改，无则增)"
		}
		_, r, err := prompt.Run()
		if err != nil {
			warning("Prompt failed %v\n", err)
			continue
		}
		set.Op = opIndex[r]
		break
	}
	if set.Op == OverWrite {
		selector := append(title, "继续")
		selector[2] = "继续"
		for {
			r := columnSelect(selector)
			if r != "继续" {
				set.OverWriteJudColumns = append(set.OverWriteJudColumns, r)
				selector = removeElement(selector, r)
				continue
			}
			break
		}
	}
	return set
}

func columnSelect(selector []string) string {
	prompt := promptui.Select{
		Label: "请选择判重字段（可以选择多个，`继续`选项表示选择完了）",
		Items: selector,
		Size:  len(selector) + 1,
	}
	_, r, err := prompt.Run()
	if err != nil {
		fatal("Prompt failed %v\n", err)

	}
	return r
}

func removeElement(slice []string, element string) []string {
	result := slice[:0] // 创建一个空切片，与原切片共享底层数组
	for _, v := range slice {
		if v != element {
			result = append(result, v)
		}
	}
	return result
}
