package sourcemap

import (
	
	
	

	
)

type fn func(m *mappings) (fn, error)

type mapping struct {
	genLine      int32
	genColumn    int32
	sourcesInd   int32
	sourceLine   int32
	sourceColumn int32
	namesInd     int32
}

type mappings struct {
	rd  *strings.Reader
	dec base64vlq.Decoder

	hasValue bool
	hasName  bool
	value    mapping

	values []mapping
}

func parseMappings( string) ([]mapping, error) {
	if  == "" {
		return nil, errors.New("sourcemap: mappings are empty")
	}

	 := strings.NewReader()
	 := &mappings{
		rd:  ,
		dec: base64vlq.NewDecoder(),

		values: make([]mapping, 0, mappingsNumber()),
	}
	.value.genLine = 1
	.value.sourceLine = 1

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

	 := .values
	.values = nil
	return , nil
}

func mappingsNumber( string) int {
	return strings.Count(, ",") + strings.Count(, ";")
}

func ( *mappings) () error {
	 := parseGenCol
	for {
		,  := .rd.ReadByte()
		if  == io.EOF {
			.pushValue()
			return nil
		}
		if  != nil {
			return 
		}

		switch  {
		case ',':
			.pushValue()
			 = parseGenCol
		case ';':
			.pushValue()

			.value.genLine++
			.value.genColumn = 0

			 = parseGenCol
		default:
			 := .rd.UnreadByte()
			if  != nil {
				return 
			}

			,  = ()
			if  != nil {
				return 
			}
			.hasValue = true
		}
	}
}

func parseGenCol( *mappings) (fn, error) {
	,  := .dec.Decode()
	if  != nil {
		return nil, 
	}
	.value.genColumn += 
	return parseSourcesInd, nil
}

func parseSourcesInd( *mappings) (fn, error) {
	,  := .dec.Decode()
	if  != nil {
		return nil, 
	}
	.value.sourcesInd += 
	return parseSourceLine, nil
}

func parseSourceLine( *mappings) (fn, error) {
	,  := .dec.Decode()
	if  != nil {
		return nil, 
	}
	.value.sourceLine += 
	return parseSourceCol, nil
}

func parseSourceCol( *mappings) (fn, error) {
	,  := .dec.Decode()
	if  != nil {
		return nil, 
	}
	.value.sourceColumn += 
	return parseNamesInd, nil
}

func parseNamesInd( *mappings) (fn, error) {
	,  := .dec.Decode()
	if  != nil {
		return nil, 
	}
	.hasName = true
	.value.namesInd += 
	return parseGenCol, nil
}

func ( *mappings) () {
	if !.hasValue {
		return
	}
	.hasValue = false
	if .hasName {
		.values = append(.values, .value)
		.hasName = false
	} else {
		.values = append(.values, mapping{
			genLine:      .value.genLine,
			genColumn:    .value.genColumn,
			sourcesInd:   .value.sourcesInd,
			sourceLine:   .value.sourceLine,
			sourceColumn: .value.sourceColumn,
			namesInd:     -1,
		})
	}
}