// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package precis

import (
	
	
	
	
	
)

// An Option is used to define the behavior and rules of a Profile.
type Option func(*options)

type options struct {
	// Preparation options
	foldWidth bool

	// Enforcement options
	asciiLower    bool
	cases         transform.SpanningTransformer
	disallow      runes.Set
	norm          transform.SpanningTransformer
	additional    []func() transform.SpanningTransformer
	width         transform.SpanningTransformer
	disallowEmpty bool
	bidiRule      bool
	repeat        bool

	// Comparison options
	ignorecase bool
}

func getOpts( ...Option) ( options) {
	for ,  := range  {
		(&)
	}
	// Using a SpanningTransformer, instead of norm.Form prevents an allocation
	// down the road.
	if .norm == nil {
		.norm = norm.NFC
	}
	return
}

var (
	// The IgnoreCase option causes the profile to perform a case insensitive
	// comparison during the PRECIS comparison step.
	IgnoreCase Option = ignoreCase

	// The FoldWidth option causes the profile to map non-canonical wide and
	// narrow variants to their decomposition mapping. This is useful for
	// profiles that are based on the identifier class which would otherwise
	// disallow such characters.
	FoldWidth Option = foldWidth

	// The DisallowEmpty option causes the enforcement step to return an error if
	// the resulting string would be empty.
	DisallowEmpty Option = disallowEmpty

	// The BidiRule option causes the Bidi Rule defined in RFC 5893 to be
	// applied.
	BidiRule Option = bidiRule
)

var (
	ignoreCase = func( *options) {
		.ignorecase = true
	}
	foldWidth = func( *options) {
		.foldWidth = true
	}
	disallowEmpty = func( *options) {
		.disallowEmpty = true
	}
	bidiRule = func( *options) {
		.bidiRule = true
	}
	repeat = func( *options) {
		.repeat = true
	}
)

// TODO: move this logic to package transform

type spanWrap struct{ transform.Transformer }

func ( spanWrap) ( []byte,  bool) ( int,  error) {
	return 0, transform.ErrEndOfSpan
}

// TODO: allow different types? For instance:
//     func() transform.Transformer
//     func() transform.SpanningTransformer
//     func([]byte) bool  // validation only
//
// Also, would be great if we could detect if a transformer is reentrant.

// The AdditionalMapping option defines the additional mapping rule for the
// Profile by applying Transformer's in sequence.
func ( ...func() transform.Transformer) Option {
	return func( *options) {
		for ,  := range  {
			 := func() transform.SpanningTransformer {
				return ().(transform.SpanningTransformer)
			}
			if ,  := ().(transform.SpanningTransformer); ! {
				 = func() transform.SpanningTransformer {
					return spanWrap{()}
				}
			}
			.additional = append(.additional, )
		}
	}
}

// The Norm option defines a Profile's normalization rule. Defaults to NFC.
func ( norm.Form) Option {
	return func( *options) {
		.norm = 
	}
}

// The FoldCase option defines a Profile's case mapping rule. Options can be
// provided to determine the type of case folding used.
func ( ...cases.Option) Option {
	return func( *options) {
		.asciiLower = true
		.cases = cases.Fold(...)
	}
}

// The LowerCase option defines a Profile's case mapping rule. Options can be
// provided to determine the type of case folding used.
func ( ...cases.Option) Option {
	return func( *options) {
		.asciiLower = true
		if len() == 0 {
			.cases = cases.Lower(language.Und, cases.HandleFinalSigma(false))
			return
		}

		 = append([]cases.Option{cases.HandleFinalSigma(false)}, ...)
		.cases = cases.Lower(language.Und, ...)
	}
}

// The Disallow option further restricts a Profile's allowed characters beyond
// what is disallowed by the underlying string class.
func ( runes.Set) Option {
	return func( *options) {
		.disallow = 
	}
}