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

import (
	
	

	
)

// pathElem is one step of a VariantPath: an object field when isField is set, else an array index.
type pathElem struct {
	name    string
	index   int
	isField bool
}

// VariantPath is an ordered list of steps to navigate into a variant value. The
// zero value is the root path; extend it with Field and Index.
type VariantPath struct {
	elems []pathElem
}

// Field returns a copy of the path with an object-field step appended.
func ( VariantPath) ( string) VariantPath {
	return VariantPath{elems: append(.grow(), pathElem{name: , isField: true})}
}

// Index returns a copy of the path with an array-index step appended.
func ( VariantPath) ( int) VariantPath {
	return VariantPath{elems: append(.grow(), pathElem{index: })}
}

// Join returns a copy of the path with other's steps appended.
func ( VariantPath) ( VariantPath) VariantPath {
	return VariantPath{elems: append(.grow(), .elems...)}
}

func ( VariantPath) () []pathElem {
	return append(make([]pathElem, 0, len(.elems)+1), .elems...)
}

// Len returns the number of steps in the path.
func ( VariantPath) () int { return len(.elems) }

// StepAt returns the i-th step's name and index, with isField true for an object-field step.
func ( VariantPath) ( int) ( string,  int,  bool) {
	 := .elems[]

	return .name, .index, .isField
}

// GetByPath navigates path into v and returns the leaf value. found is false when
// the path is cleanly absent (a missing object field, or an out-of-range or
// non-array index). It returns an error for a type error (a field step into a
// non-object) or corrupt data (a field id not present in the metadata).
func ( Value) ( VariantPath) ( Value,  bool,  error) {
	 := 
	for ,  := range .elems {
		if .isField {
			,  := .Value().(ObjectValue)
			if ! {
				return Value{}, false, fmt.Errorf("%w: variant path field %q applied to non-object", arrow.ErrInvalid, .name)
			}
			,  := .ValueByKey(.name)
			if  != nil {
				if errors.Is(, arrow.ErrNotFound) {
					return Value{}, false, nil
				}

				return Value{}, false, 
			}
			 = .Value

			continue
		}

		,  := .Value().(ArrayValue)
		if ! {
			return Value{}, false, nil
		}
		if .index < 0 || uint64(.index) >= uint64(.Len()) {
			return Value{}, false, nil
		}
		,  := .Value(uint32(.index))
		if  != nil {
			return Value{}, false, nil
		}
		 = 
	}

	return , true, nil
}