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

import (
	
	

	
	
	
	
	
)

type decimalScalar[ interface {
	decimal.DecimalTypes
	decimal.Num[]
}] struct {
	scalar
	Value 
}

type (
	Decimal32 = decimalScalar[decimal.Decimal32]
	Decimal64 = decimalScalar[decimal.Decimal64]
)

func ( decimal.Decimal32,  arrow.DataType) *Decimal32 {
	return &Decimal32{scalar{, true}, }
}

func ( decimal.Decimal64,  arrow.DataType) *Decimal64 {
	return &Decimal64{scalar{, true}, }
}

func ( *decimalScalar[]) () []byte {
	return unsafe.Slice((*byte)(unsafe.Pointer(&.Value)), unsafe.Sizeof(.Value))
}

func ( *decimalScalar[]) () interface{} { return .Value }

func ( *decimalScalar[]) ( Scalar) bool {
	return .Value == .(*decimalScalar[]).Value
}

func ( *decimalScalar[]) () string {
	if !.Valid {
		return "null"
	}
	,  := .CastTo(arrow.BinaryTypes.String)
	if  != nil {
		return "..."
	}
	return string(.(*String).Value.Bytes())
}

// CastTo casts to string or to a decimal type at least as wide as the
// scalar's own type.
func ( *decimalScalar[]) ( arrow.DataType) (Scalar, error) {
	if !.Valid {
		return MakeNullScalar(), nil
	}

	 := .Type.(arrow.DecimalType)
	if .ID() == arrow.STRING {
		,  := utils.FormatDecimalG(.Value, )
		if  != nil {
			return nil, 
		}
		return NewStringScalar(), nil
	}

	,  := .(arrow.DecimalType)
	if ! || .BitWidth() < .BitWidth() {
		return nil, fmt.Errorf("cannot cast non-nil %s scalar to type %s", .Type, )
	}
	,  := toDecimal256(.Value).Rescale(.GetScale(), .GetScale())
	if  != nil {
		return nil, 
	}
	if !.FitsInPrecision(.GetPrecision()) {
		return nil, fmt.Errorf("%s value %v will not fit in new precision %d", , , .GetPrecision())
	}

	switch .ID() {
	case arrow.DECIMAL32:
		return NewDecimal32Scalar(decimal.Decimal32(.LowBits()), ), nil
	case arrow.DECIMAL64:
		return NewDecimal64Scalar(decimal.Decimal64(.LowBits()), ), nil
	case arrow.DECIMAL128:
		return NewDecimal128Scalar(decimal128.New(int64(.Array()[1]), .LowBits()), ), nil
	default:
		return NewDecimal256Scalar(, ), nil
	}
}

func toDecimal256[ decimal.DecimalTypes]( ) decimal256.Num {
	switch v := any().(type) {
	case decimal.Decimal32:
		return decimal256.FromI64(int64())
	case decimal.Decimal64:
		return decimal256.FromI64(int64())
	case decimal128.Num:
		return decimal256.FromDecimal128()
	default:
		return .(decimal256.Num)
	}
}