// 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 array

import (
	
	
	
	

	
	
	
	
)

// A FixedSizeBinaryBuilder is used to build a FixedSizeBinary array using the Append methods.
type FixedSizeBinaryBuilder struct {
	builder

	dtype  *arrow.FixedSizeBinaryType
	values *byteBufferBuilder
}

func ( memory.Allocator,  *arrow.FixedSizeBinaryType) *FixedSizeBinaryBuilder {
	 := &FixedSizeBinaryBuilder{
		builder: builder{mem: },
		dtype:   ,
		values:  newByteBufferBuilder(),
	}
	.refCount.Add(1)
	return 
}

func ( *FixedSizeBinaryBuilder) () arrow.DataType { return .dtype }

// Release decreases the reference count by 1.
// When the reference count goes to zero, the memory is freed.
// Release may be called simultaneously from multiple goroutines.
func ( *FixedSizeBinaryBuilder) () {
	debug.Assert(.refCount.Load() > 0, "too many releases")

	if .refCount.Add(-1) == 0 {
		if .nullBitmap != nil {
			.nullBitmap.Release()
			.nullBitmap = nil
		}
		if .values != nil {
			.values.Release()
			.values = nil
		}
	}
}

func ( *FixedSizeBinaryBuilder) ( []byte) {
	if len() != .dtype.ByteWidth {
		// TODO(alexandre): should we return an error instead?
		panic("len(v) != b.dtype.ByteWidth")
	}

	.Reserve(1)
	.values.Append()
	.UnsafeAppendBoolToBitmap(true)
}

func ( *FixedSizeBinaryBuilder) () {
	.Reserve(1)
	.values.Advance(.dtype.ByteWidth)
	.UnsafeAppendBoolToBitmap(false)
}

func ( *FixedSizeBinaryBuilder) ( int) {
	for  := 0;  < ; ++ {
		.AppendNull()
	}
}

func ( *FixedSizeBinaryBuilder) () {
	.Reserve(1)
	.values.Advance(.dtype.ByteWidth)
	.UnsafeAppendBoolToBitmap(true)
}

func ( *FixedSizeBinaryBuilder) ( int) {
	for  := 0;  < ; ++ {
		.AppendEmptyValue()
	}
}

func ( *FixedSizeBinaryBuilder) ( []byte) {
	.values.unsafeAppend()
	.UnsafeAppendBoolToBitmap(true)
}

// AppendValues will append the values in the v slice. The valid slice determines which values
// in v are valid (not null). The valid slice must either be empty or be equal in length to v. If empty,
// all values in v are appended and considered valid.
func ( *FixedSizeBinaryBuilder) ( [][]byte,  []bool) {
	if len() != len() && len() != 0 {
		panic("len(v) != len(valid) && len(valid) != 0")
	}

	if len() == 0 {
		return
	}

	.Reserve(len())
	for ,  := range  {
		switch len() {
		case 0:
			.values.Advance(.dtype.ByteWidth)
		case .dtype.ByteWidth:
			.values.Append()
		default:
			panic(fmt.Errorf("array: invalid binary length (got=%d, want=%d)", len(), .dtype.ByteWidth))
		}
	}

	.unsafeAppendBoolsToBitmap(, len())
}

func ( *FixedSizeBinaryBuilder) ( int) {
	.builder.init()
	.values.resize( * .dtype.ByteWidth)
}

// Reserve ensures there is enough space for appending n elements
// by checking the capacity and calling Resize if necessary.
func ( *FixedSizeBinaryBuilder) ( int) {
	.reserve(, .Resize)
}

// Resize adjusts the space allocated by b to n elements. If n is greater than b.Cap(),
// additional memory will be allocated. If n is smaller, the allocated memory may reduced.
func ( *FixedSizeBinaryBuilder) ( int) {
	.resize(, .init)
}

// NewArray creates a FixedSizeBinary array from the memory buffers used by the
// builder and resets the FixedSizeBinaryBuilder so it can be used to build a new array.
func ( *FixedSizeBinaryBuilder) () arrow.Array {
	return .NewFixedSizeBinaryArray()
}

// NewFixedSizeBinaryArray creates a FixedSizeBinary array from the memory buffers used by the builder and resets the FixedSizeBinaryBuilder
// so it can be used to build a new array.
func ( *FixedSizeBinaryBuilder) () ( *FixedSizeBinary) {
	 := .newData()
	 = NewFixedSizeBinaryData()
	.Release()
	return
}

func ( *FixedSizeBinaryBuilder) () ( *Data) {
	 := .values.Finish()
	 = NewData(.dtype, .length, []*memory.Buffer{.nullBitmap, }, nil, .nulls, 0)

	if  != nil {
		.Release()
	}

	.reset()

	return
}

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

	,  := base64.StdEncoding.DecodeString()
	if  != nil {
		.AppendNull()
		return 
	}
	.Append()
	return nil
}

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

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

	if len() != .dtype.ByteWidth {
		return &json.UnmarshalTypeError{
			Value:  fmt.Sprint(string()),
			Type:   reflect.TypeOf([]byte{}),
			Offset: .InputOffset(),
			Struct: fmt.Sprintf("FixedSizeBinary[%d]", .dtype.ByteWidth),
		}
	}
	.Append()
	return nil
}

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

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

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

	return .Unmarshal()
}

var _ Builder = (*FixedSizeBinaryBuilder)(nil)