// Copyright © 2014 Steve Francia <spf@spf13.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

package cast

import (
	
	
	
)

// ToSliceE casts any value to a []any type.
func ( any) ([]any, error) {
	, _ = indirect()

	var  []any

	switch v := .(type) {
	case []any:
		// TODO: use slices.Clone
		return append(, ...), nil
	case []map[string]any:
		for ,  := range  {
			 = append(, )
		}

		return , nil
	default:
		return , fmt.Errorf(errorMsg, , , )
	}
}

func toSliceE[ Basic]( any) ([], error) {
	, ,  := toSliceEOk[]()
	if  != nil {
		return nil, 
	}

	if ! {
		return nil, fmt.Errorf(errorMsg, , , []{})
	}

	return , nil
}

func toSliceEOk[ Basic]( any) ([], bool, error) {
	, _ = indirect()
	if  == nil {
		return nil, true, fmt.Errorf(errorMsg, , , []{})
	}

	switch v := .(type) {
	case []:
		// TODO: clone slice
		return , true, nil
	}

	 := reflect.TypeOf().Kind()
	switch  {
	case reflect.Slice, reflect.Array:
		 := reflect.ValueOf()
		 := make([], .Len())

		for  := 0;  < .Len(); ++ {
			,  := ToE[](.Index().Interface())
			if  != nil {
				return nil, true, fmt.Errorf(errorMsg, , , []{})
			}

			[] = 
		}

		return , true, nil
	default:
		return nil, false, nil
	}
}

// ToStringSliceE casts any value to a []string type.
func ( any) ([]string, error) {
	if , ,  := toSliceEOk[string]();  {
		if  != nil {
			return nil, 
		}

		return , nil
	}

	var  []string

	switch v := .(type) {
	case string:
		return strings.Fields(), nil
	case any:
		,  := ToStringE()
		if  != nil {
			return nil, fmt.Errorf(errorMsg, , , )
		}

		return []string{}, nil
	default:
		return nil, fmt.Errorf(errorMsg, , , )
	}
}