/**
 * uses a smoothstep function
 *
 *
 */
import {PolyDictionary} from '../../../types/GlobalTypes';
import {Poly} from '../../Poly';
import {
	// isJsConnectionPointPrimitive,
	JsConnectionPointType,
	JsConnectionPointTypeFromArrayTypeMap,
} from '../utils/io/connections/Js';
import {JsLinesCollectionController} from './code/utils/JsLinesCollectionController';
// import {ShadersCollectionController} from './code/utils/ShadersCollectionController';
// import {LocalFunctionJsDefinition} from './utils/JsDefinition';
import {
	MathFunctionArg3OperationFactory,
	DEFAULT_ALLOWED_TYPES,
	// functionDefinition,
	// FUNC_ARG_NAME,
} from './_Math_Arg1Operation';

// const CLAMP_FUNCTION_NAME = 'clamp';
// const CLAMP_FUNCTION_BODY = `function ${CLAMP_FUNCTION_NAME}(src,min,max){
// 	return Math.min(Math.max(src, min), max)
// }`;
export enum SmootherstepInput {
	X = 'x',
	EDGE0 = 'edge0',
	EDGE1 = 'edge1',
}
const DefaultValues: PolyDictionary<number> = {
	[SmootherstepInput.X]: 0,
	[SmootherstepInput.EDGE0]: 0,
	[SmootherstepInput.EDGE1]: 1,
};

const FUNCTION_NAME = 'smootherstep';
export class SmootherstepJsNode extends MathFunctionArg3OperationFactory('smootherstep', {
	inputPrefix: 'in',
	out: 'smootherstep',
}) {
	protected _coreFunction(shadersCollectionController: JsLinesCollectionController) {
		const mainArg = 'x';
		const _min = this.variableForInput(shadersCollectionController, SmootherstepInput.EDGE0);
		const _max = this.variableForInput(shadersCollectionController, SmootherstepInput.EDGE1);
		Poly.namedFunctionsRegister
			.getFunction(FUNCTION_NAME, this, shadersCollectionController)
			.asString(mainArg, _min, _max);

		return FUNCTION_NAME;
	}

	override paramDefaultValue(name: string) {
		return DefaultValues[name];
	}

	protected override _expectedInputName(index: number): string {
		return [SmootherstepInput.X, SmootherstepInput.EDGE0, SmootherstepInput.EDGE1][index];
	}

	protected override _expectedInputTypes() {
		let first_input_type = this.io.connection_points.first_input_connection_type();
		const inputConnectionPoints = this.io.inputs.namedInputConnectionPoints();
		if (!inputConnectionPoints) {
			return [];
		}
		if (first_input_type) {
			if (!DEFAULT_ALLOWED_TYPES.includes(first_input_type)) {
				// if the first input type is not allowed, either leave the connection point as is,
				// or use the default if there is none
				const first_connection = inputConnectionPoints[0];
				if (first_connection) {
					first_input_type = first_connection.type();
				}
			}
		}
		const type = first_input_type || JsConnectionPointType.FLOAT;
		const boundType = JsConnectionPointTypeFromArrayTypeMap[type];
		return [type, boundType, boundType];
	}
}
