package pgtype

import (
	
	
	
	
	
	

	
)

type LsegScanner interface {
	ScanLseg(v Lseg) error
}

type LsegValuer interface {
	LsegValue() (Lseg, error)
}

type Lseg struct {
	P     [2]Vec2
	Valid bool
}

func ( *Lseg) ( Lseg) error {
	* = 
	return nil
}

func ( Lseg) () (Lseg, error) {
	return , nil
}

// Scan implements the database/sql Scanner interface.
func ( *Lseg) ( any) error {
	if  == nil {
		* = Lseg{}
		return nil
	}

	switch src := .(type) {
	case string:
		return scanPlanTextAnyToLsegScanner{}.Scan([]byte(), )
	}

	return fmt.Errorf("cannot scan %T", )
}

// Value implements the database/sql/driver Valuer interface.
func ( Lseg) () (driver.Value, error) {
	if !.Valid {
		return nil, nil
	}

	,  := LsegCodec{}.PlanEncode(nil, 0, TextFormatCode, ).Encode(, nil)
	if  != nil {
		return nil, 
	}
	return string(), 
}

type LsegCodec struct{}

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

func (LsegCodec) () int16 {
	return BinaryFormatCode
}

func (LsegCodec) ( *Map,  uint32,  int16,  any) EncodePlan {
	if ,  := .(LsegValuer); ! {
		return nil
	}

	switch  {
	case BinaryFormatCode:
		return encodePlanLsegCodecBinary{}
	case TextFormatCode:
		return encodePlanLsegCodecText{}
	}

	return nil
}

type encodePlanLsegCodecBinary struct{}

func (encodePlanLsegCodecBinary) ( any,  []byte) ( []byte,  error) {
	,  := .(LsegValuer).LsegValue()
	if  != nil {
		return nil, 
	}

	if !.Valid {
		return nil, nil
	}

	 = pgio.AppendUint64(, math.Float64bits(.P[0].X))
	 = pgio.AppendUint64(, math.Float64bits(.P[0].Y))
	 = pgio.AppendUint64(, math.Float64bits(.P[1].X))
	 = pgio.AppendUint64(, math.Float64bits(.P[1].Y))
	return , nil
}

type encodePlanLsegCodecText struct{}

func (encodePlanLsegCodecText) ( any,  []byte) ( []byte,  error) {
	,  := .(LsegValuer).LsegValue()
	if  != nil {
		return nil, 
	}

	if !.Valid {
		return nil, nil
	}

	 = append(, fmt.Sprintf(`[(%s,%s),(%s,%s)]`,
		strconv.FormatFloat(.P[0].X, 'f', -1, 64),
		strconv.FormatFloat(.P[0].Y, 'f', -1, 64),
		strconv.FormatFloat(.P[1].X, 'f', -1, 64),
		strconv.FormatFloat(.P[1].Y, 'f', -1, 64),
	)...)
	return , nil
}

func (LsegCodec) ( *Map,  uint32,  int16,  any) ScanPlan {

	switch  {
	case BinaryFormatCode:
		switch .(type) {
		case LsegScanner:
			return scanPlanBinaryLsegToLsegScanner{}
		}
	case TextFormatCode:
		switch .(type) {
		case LsegScanner:
			return scanPlanTextAnyToLsegScanner{}
		}
	}

	return nil
}

type scanPlanBinaryLsegToLsegScanner struct{}

func (scanPlanBinaryLsegToLsegScanner) ( []byte,  any) error {
	 := ().(LsegScanner)

	if  == nil {
		return .ScanLseg(Lseg{})
	}

	if len() != 32 {
		return fmt.Errorf("invalid length for lseg: %v", len())
	}

	 := binary.BigEndian.Uint64()
	 := binary.BigEndian.Uint64([8:])
	 := binary.BigEndian.Uint64([16:])
	 := binary.BigEndian.Uint64([24:])

	return .ScanLseg(Lseg{
		P: [2]Vec2{
			{math.Float64frombits(), math.Float64frombits()},
			{math.Float64frombits(), math.Float64frombits()},
		},
		Valid: true,
	})
}

type scanPlanTextAnyToLsegScanner struct{}

func (scanPlanTextAnyToLsegScanner) ( []byte,  any) error {
	 := ().(LsegScanner)

	if  == nil {
		return .ScanLseg(Lseg{})
	}

	if len() < 11 {
		return fmt.Errorf("invalid length for lseg: %v", len())
	}

	 := string([2:])

	var  int
	 = strings.IndexByte(, ',')

	,  := strconv.ParseFloat([:], 64)
	if  != nil {
		return 
	}

	 = [+1:]
	 = strings.IndexByte(, ')')

	,  := strconv.ParseFloat([:], 64)
	if  != nil {
		return 
	}

	 = [+3:]
	 = strings.IndexByte(, ',')

	,  := strconv.ParseFloat([:], 64)
	if  != nil {
		return 
	}

	 = [+1 : len()-2]

	,  := strconv.ParseFloat(, 64)
	if  != nil {
		return 
	}

	return .ScanLseg(Lseg{P: [2]Vec2{{, }, {, }}, Valid: true})
}

func ( LsegCodec) ( *Map,  uint32,  int16,  []byte) (driver.Value, error) {
	return codecDecodeToTextFormat(, , , , )
}

func ( LsegCodec) ( *Map,  uint32,  int16,  []byte) (any, error) {
	if  == nil {
		return nil, nil
	}

	var  Lseg
	 := codecScan(, , , , , &)
	if  != nil {
		return nil, 
	}
	return , nil
}