// Package util ...
//
// Description : util ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-03-30 14:35
package util

import (
	"encoding/json"
	"fmt"
	"reflect"
	"strings"
)

type array struct {
}

// In 判断指定数据是否在目标数据集中, 不存在返回 -1 , 存在时返回数据对应的索引
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:38 2023/3/30
func (a *array) In(search interface{}, source interface{}) int {
	if nil == source {
		return -1
	}
	sourceType := reflect.TypeOf(source).Kind()
	if sourceType != reflect.Array && sourceType != reflect.Slice {
		return -1
	}

	byteData, _ := json.Marshal(source)
	var (
		formatSourceData []interface{}
	)

	_ = JSON.UnmarshalWithNumber(byteData, &formatSourceData)

	if nil == search {
		for idx, item := range formatSourceData {
			if item == nil {
				return idx
			}
		}
		return -1
	}
	searchType := reflect.TypeOf(search).Kind()
	for idx, item := range formatSourceData {
		itemKind := reflect.TypeOf(item).Kind()
		if searchType != itemKind {
			// 类型不同, 检测是否为数字
			if strings.Contains(searchType.String(), "int") || strings.Contains(searchType.String(), "float") {
				// 查询的是数字
				if _, ok := item.(json.Number); ok {
					if fmt.Sprintf("%v", search) == fmt.Sprintf("%v", item) {
						return idx
					}
				}
			}
			continue
		}
		if fmt.Sprintf("%v", search) == fmt.Sprintf("%v", item) {
			return idx
		}
	}
	return -1
}

// InAny In 函数的简化写法
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:25 2023/3/30
func (a *array) InAny(search interface{}, source ...interface{}) int {
	return a.In(search, source)
}