// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package extensions

import (
	
	
	
	

	
	
	
	
	
	
)

type UUIDBuilder struct {
	*array.ExtensionBuilder
}

// NewUUIDBuilder creates a new UUIDBuilder, exposing a convenient and efficient interface
// for writing uuid.UUID (or [16]byte) values to the underlying FixedSizeBinary storage array.
func ( memory.Allocator) *UUIDBuilder {
	return &UUIDBuilder{ExtensionBuilder: array.NewExtensionBuilder(, NewUUIDType())}
}

func ( *UUIDBuilder) ( uuid.UUID) {
	.AppendBytes()
}

func ( *UUIDBuilder) ( [16]byte) {
	.ExtensionBuilder.Builder.(*array.FixedSizeBinaryBuilder).Append([:])
}

func ( *UUIDBuilder) ( uuid.UUID) {
	.ExtensionBuilder.Builder.(*array.FixedSizeBinaryBuilder).UnsafeAppend([:])
}

func ( *UUIDBuilder) ( string) error {
	if  == array.NullValueStr {
		.AppendNull()
		return nil
	}

	,  := uuid.Parse()
	if  != nil {
		return 
	}

	.Append()
	return nil
}

func ( *UUIDBuilder) ( []uuid.UUID,  []bool) {
	if len() != len() && len() != 0 {
		panic("len(v) != len(valid) && len(valid) != 0")
	}

	 := make([][]byte, len())
	for  := range  {
		if len() > 0 && ![] {
			continue
		}
		[] = [][:]
	}
	.ExtensionBuilder.Builder.(*array.FixedSizeBinaryBuilder).AppendValues(, )
}

func ( *UUIDBuilder) ( *json.Decoder) error {
	,  := .Token()
	if  != nil {
		return 
	}

	var  uuid.UUID
	switch v := .(type) {
	case string:
		,  = uuid.Parse()
		if  != nil {
			return 
		}
	case []byte:
		,  = uuid.ParseBytes()
		if  != nil {
			return 
		}
	case nil:
		.AppendNull()
		return nil
	default:
		return &json.UnmarshalTypeError{
			Value:  fmt.Sprint(),
			Type:   reflect.TypeOf([]byte{}),
			Offset: .InputOffset(),
			Struct: fmt.Sprintf("FixedSizeBinary[%d]", 16),
		}
	}

	.Append()
	return nil
}

func ( *UUIDBuilder) ( *json.Decoder) error {
	for .More() {
		if  := .UnmarshalOne();  != nil {
			return 
		}
	}
	return nil
}

func ( *UUIDBuilder) ( []byte) error {
	 := json.NewDecoder(bytes.NewReader())
	,  := .Token()
	if  != nil {
		return 
	}

	if ,  := .(json.Delim); ! ||  != '[' {
		return fmt.Errorf("uuid builder must unpack from json array, found %s", )
	}

	return .Unmarshal()
}

// UUIDArray is a simple array which is a FixedSizeBinary(16)
type UUIDArray struct {
	array.ExtensionArrayBase
}

func ( *UUIDArray) () string {
	 := .Storage().(*array.FixedSizeBinary)
	 := new(strings.Builder)
	.WriteString("[")
	for  := 0;  < .Len(); ++ {
		if  > 0 {
			.WriteString(" ")
		}
		switch {
		case .IsNull():
			.WriteString(array.NullValueStr)
		default:
			fmt.Fprintf(, "%q", .Value())
		}
	}
	.WriteString("]")
	return .String()
}

func ( *UUIDArray) ( int) uuid.UUID {
	if .IsNull() {
		return uuid.Nil
	}
	return uuid.Must(uuid.FromBytes(.Storage().(*array.FixedSizeBinary).Value()))
}

func ( *UUIDArray) () []uuid.UUID {
	 := make([]uuid.UUID, .Len())
	for  := range  {
		[] = .Value()
	}
	return 
}

func ( *UUIDArray) ( int) string {
	switch {
	case .IsNull():
		return array.NullValueStr
	default:
		return .Value().String()
	}
}

func ( *UUIDArray) () ([]byte, error) {
	 := make([]any, .Len())
	for  := range  {
		[] = .GetOneForMarshal()
	}
	return json.Marshal()
}

func ( *UUIDArray) ( int) interface{} {
	if .IsValid() {
		return .Value()
	}
	return nil
}

// UUIDType is a simple extension type that represents a FixedSizeBinary(16)
// to be used for representing UUIDs
type UUIDType struct {
	arrow.ExtensionBase
}

// ParquetLogicalType implements pqarrow.ExtensionCustomParquetType.
func ( *UUIDType) () schema.LogicalType {
	return schema.UUIDLogicalType{}
}

// NewUUIDType is a convenience function to create an instance of UUIDType
// with the correct storage type
func () *UUIDType {
	return &UUIDType{ExtensionBase: arrow.ExtensionBase{Storage: &arrow.FixedSizeBinaryType{ByteWidth: 16}}}
}

// ArrayType returns TypeOf(UUIDArray{}) for constructing UUID arrays
func (*UUIDType) () reflect.Type {
	return reflect.TypeOf(UUIDArray{})
}

func (*UUIDType) () string {
	return "arrow.uuid"
}

func (*UUIDType) () int    { return 16 }
func (*UUIDType) () int { return 128 }

func ( *UUIDType) () string {
	return fmt.Sprintf("extension<%s>", .ExtensionName())
}

func ( *UUIDType) () ([]byte, error) {
	return []byte(fmt.Sprintf(`{"name":"%s","metadata":%s}`, .ExtensionName(), .Serialize())), nil
}

func (*UUIDType) () string {
	return ""
}

// Deserialize expects storageType to be FixedSizeBinaryType{ByteWidth: 16}
func (*UUIDType) ( arrow.DataType,  string) (arrow.ExtensionType, error) {
	if !arrow.TypeEqual(, &arrow.FixedSizeBinaryType{ByteWidth: 16}) {
		return nil, fmt.Errorf("invalid storage type for UUIDType: %s", .Name())
	}
	return NewUUIDType(), nil
}

// ExtensionEquals returns true if both extensions have the same name
func ( *UUIDType) ( arrow.ExtensionType) bool {
	return .ExtensionName() == .ExtensionName()
}

func (*UUIDType) ( memory.Allocator) array.Builder {
	return NewUUIDBuilder()
}

var (
	_ arrow.ExtensionType          = (*UUIDType)(nil)
	_ array.CustomExtensionBuilder = (*UUIDType)(nil)
	_ array.ExtensionArray         = (*UUIDArray)(nil)
	_ array.Builder                = (*UUIDBuilder)(nil)

	_ arrow.FixedWidthDataType = (*UUIDType)(nil)
)