package regexp2

import (
	
)

type RuntimeEngineData struct {
	Caps               map[int]int        // capnum->index
	CapNames           map[string]int     // cap group name -> index
	CapsList           []string           // sorted list of capture group names
	CapSize            int                // size of the capture array
	FindFirstChar      func(*Runner) bool // generated candidate search
	Execute            func(*Runner) error
	ExecuteQuick       func(*Runner) error // optional bool-only execution with unobservable captures removed
	StringPrefixFilter StringPrefixFilter  // optional pre-decode candidate search for string input
}

type cacheKey struct {
	pattern                  string
	opt                      RegexOptions
	maintainCaptureOrder     bool
	maxBacktrackingStackSize int
}

func ( string,  RuntimeEngineData,  ...CompileOption) {
	 := newCompileConfig()
	enginesMu.Lock()
	engines[cacheKeyFromConfig(, )] = 
	enginesMu.Unlock()
}

func newEngineRegexp( string,  compileConfig,  RuntimeEngineData) *Regexp {
	 := &Regexp{
		pattern:            ,
		options:            .regexOptions,
		debug:              .debug,
		caps:               .Caps,
		capnames:           .CapNames,
		capslist:           .CapsList,
		capsize:            .CapSize,
		MatchTimeout:       DefaultMatchTimeout,
		optimizations:      .optimizations,
		findFirstChar:      .FindFirstChar,
		execute:            .Execute,
		executeQuick:       .ExecuteQuick,
		stringPrefixFilter: .StringPrefixFilter,
	}
	.initCaches()
	return 
}

func getEngineRegexp( string,  compileConfig) *Regexp {
	enginesMu.RLock()
	,  := engines[cacheKeyFromConfig(, )]
	enginesMu.RUnlock()
	if ! {
		return nil
	}
	return newEngineRegexp(, , )
}

func cacheKeyFromConfig( string,  compileConfig) cacheKey {
	return cacheKey{
		pattern:                  ,
		opt:                      .regexOptions,
		maintainCaptureOrder:     .maintainCaptureOrder,
		maxBacktrackingStackSize: .optimizations.MaxBacktrackingStackSize,
	}
}

var (
	enginesMu sync.RWMutex
	engines   = map[cacheKey]RuntimeEngineData{}
)