package highlight

import (
	

	
)

const latesBufferSize = 128

// Highlighter is a code highlighter that can deliver results asynchronously.
type Highlighter struct {
	cfg   Config
	state state
	lates chan struct{}
}

type state struct {
	sync.Mutex
	code       string
	styledCode ui.Text
	errors     []error
}

func ( Config) *Highlighter {
	return &Highlighter{, state{}, make(chan struct{}, latesBufferSize)}
}

// Get returns the highlighted code and static errors found in the code.
func ( *Highlighter) ( string) (ui.Text, []error) {
	.state.Lock()
	defer .state.Unlock()
	if  == .state.code {
		return .state.styledCode, .state.errors
	}

	 := func( ui.Text) {
		.state.Lock()
		if .state.code !=  {
			// Late result was delivered after code has changed. Unlock and
			// return.
			.state.Unlock()
			return
		}
		.state.styledCode = 
		// The channel send below might block, so unlock the state first.
		.state.Unlock()
		.lates <- struct{}{}
	}

	,  := highlight(, .cfg, )

	.state.code = 
	.state.styledCode = 
	.state.errors = 
	return , 
}

// LateUpdates returns a channel for notifying late updates.
func ( *Highlighter) () <-chan struct{} {
	return .lates
}