package pgtype

import (
	
	
)

type LtreeCodec struct{}

func ( LtreeCodec) ( int16) bool {
	return  == TextFormatCode ||  == BinaryFormatCode
}

// PreferredFormat returns the preferred format.
func ( LtreeCodec) () int16 {
	return TextFormatCode
}

// PlanEncode returns an EncodePlan for encoding value into PostgreSQL format for oid and format. If no plan can be
// found then nil is returned.
func ( LtreeCodec) ( *Map,  uint32,  int16,  any) EncodePlan {
	switch  {
	case TextFormatCode:
		return (TextCodec)().PlanEncode(, , , )
	case BinaryFormatCode:
		switch .(type) {
		case string:
			return encodeLtreeCodecBinaryString{}
		case []byte:
			return encodeLtreeCodecBinaryByteSlice{}
		case TextValuer:
			return encodeLtreeCodecBinaryTextValuer{}
		}
	}

	return nil
}

type encodeLtreeCodecBinaryString struct{}

func (encodeLtreeCodecBinaryString) ( any,  []byte) ( []byte,  error) {
	 := .(string)
	 = append(, 1)
	return append(, ...), nil
}

type encodeLtreeCodecBinaryByteSlice struct{}

func (encodeLtreeCodecBinaryByteSlice) ( any,  []byte) ( []byte,  error) {
	 := .([]byte)
	 = append(, 1)
	return append(, ...), nil
}

type encodeLtreeCodecBinaryTextValuer struct{}

func (encodeLtreeCodecBinaryTextValuer) ( any,  []byte) ( []byte,  error) {
	,  := .(TextValuer).TextValue()
	if  != nil {
		return nil, 
	}
	if !.Valid {
		return nil, nil
	}

	 = append(, 1)
	return append(, .String...), nil
}

// PlanScan returns a ScanPlan for scanning a PostgreSQL value into a destination with the same type as target. If
// no plan can be found then nil is returned.
func ( LtreeCodec) ( *Map,  uint32,  int16,  any) ScanPlan {
	switch  {
	case TextFormatCode:
		return (TextCodec)().PlanScan(, , , )
	case BinaryFormatCode:
		switch .(type) {
		case *string:
			return scanPlanBinaryLtreeToString{}
		case TextScanner:
			return scanPlanBinaryLtreeToTextScanner{}
		}
	}

	return nil
}

type scanPlanBinaryLtreeToString struct{}

func (scanPlanBinaryLtreeToString) ( []byte,  any) error {
	 := [0]
	if  != 1 {
		return fmt.Errorf("unsupported ltree version %d", )
	}

	 := ().(*string)
	* = string([1:])

	return nil
}

type scanPlanBinaryLtreeToTextScanner struct{}

func (scanPlanBinaryLtreeToTextScanner) ( []byte,  any) error {
	 := [0]
	if  != 1 {
		return fmt.Errorf("unsupported ltree version %d", )
	}

	 := ().(TextScanner)
	return .ScanText(Text{String: string([1:]), Valid: true})
}

// DecodeDatabaseSQLValue returns src decoded into a value compatible with the sql.Scanner interface.
func ( LtreeCodec) ( *Map,  uint32,  int16,  []byte) (driver.Value, error) {
	return (TextCodec)().DecodeDatabaseSQLValue(, , , )
}

// DecodeValue returns src decoded into its default format.
func ( LtreeCodec) ( *Map,  uint32,  int16,  []byte) (any, error) {
	return (TextCodec)().DecodeValue(, , , )
}