// Copyright (c) 2018-2022 MathJax Consortium
// SPDX-License-Identifier: Apache-2.0
// Source: physics/PhysicsMethods.ts vectorApplication, OperatorApplication and VectorOperator.

package tex

import (
	

	
)

type physicsOperatorApplication struct {
	operator string
	fences   string
	vector   bool
}

type physicsApplicationArgument struct {
	name, fences string
}

// The two source maps share vectorApplication but select different stack items:
// OperatorApplication emits FnItem; VectorOperator emits completed MML.
var physicsApplicationOperators = func() map[string]physicsOperatorApplication {
	 := make(map[string]physicsOperatorApplication)
	for ,  := range mjSourcePhysicsMaps {
		if .Name != "Physics-expressions-macros" && .Name != "Physics-vector-macros" {
			continue
		}
		for ,  := range .Entries {
			,  := .Value.(mjSourceList)
			if ! || len() < 2 || ([0] != "OperatorApplication" && [0] != "VectorOperator") {
				continue
			}
			 := physicsOperatorApplication{operator: [1].(string), vector: [0] == "VectorOperator"}
			for ,  := range [2:] {
				.fences += .(string)
			}
			[.Name] = 
		}
	}
	return 
}()

func ( *parser) ( string,  physicsOperatorApplication,  **derivativeAutoOpen) ([]*mml.Node, error) {
	// The operator is a fresh TexParser result, not a fixed token. Its inner
	// commands may have been redefined, and errors occur before GetArgument.
	,  := .parseChild(.operator)
	if  != nil {
		return nil, 
	}
	.commandNamedFunction = !.vector
	// Deliver the source item before inspecting the caller's argument. A
	// recipient such as SubsupItem accepts MML but rejects a pending FnItem
	// before a malformed brace is scanned.
	* = &derivativeAutoOpen{application: &physicsApplicationArgument{, .fences}}
	return []*mml.Node{}, nil
}

func ( *physicsApplicationArgument) ( *parser,  *derivativeAutoOpen) (bool, error) {
	for .pos < len(.source) && isPrimeSpace(.peekRune()) {
		.consumeRune()
	}
	if .pos == len(.source) {
		return false, nil
	}
	 := .source[.pos]
	 := strings.ContainsRune(.fences, rune())
	if  == '{' {
		, ,  := .readArgument(.name, false)
		if  != nil {
			return false, 
		}
		,  := "", ""
		if  {
			,  = "\\left\\{", "\\right\\}"
		}
		// The braces are consumed, not retained as a lexical group. Parse the
		// replacement and caller suffix together so fonts and macros continue.
		.source, .pos = +" "++" "++.source[.pos:], 0
	} else if  {
		 := byte(')')
		if  == '[' {
			 = ']'
		}
		.open, .closer = , 
		return true, nil
	}
	return false, nil
}