// lib/javascript/quick-js/quick-js.js
// lib/javascript/quick-js/libquickjs.js
// Test JavaScript functions for QuickJS integration

// Test: return simple string
function testReturnString() {
	return 'hello world ' + Math.random()
}

// Test: process parameter string
function testBigParam(paramStr) {
	if (!paramStr) {
		return 'quick-js: null'
	}
	if (typeof paramStr !== 'string') {
		return 'quick-js: type=' + typeof paramStr
	}
	return 'quick-js: ' + paramStr.length + ' ' + paramStr
}

// Test inline code evaluation
function testInlineCode() {
	return 42 + 10
}

// Test array operations
function testArrayOps() {
	const arr = [1, 2, 3, 4, 5]
	let sum = 0
	for (let i = 0; i < arr.length; i = i + 1) {
		sum = sum + arr[i]
	}
	return sum
}

// Test complex inline code with multiple operations
function testComplexInline() {
	const base = 100
	const multiplier = 2
	const offset = 50
	let result = base
	result = result * multiplier
	result = result + offset
	return result
}

// Test nested function calls
function testNestedCalls() {
	function add(a, b) {
		return a + b
	}
	function multiply(x, y) {
		return x * y
	}
	const a = 5
	const b = 3
	const c = 2
	return multiply(add(a, b), c)
}

// Test conditional logic
function testConditionals() {
	const value = 75
	let result = 0
	if (value > 50) {
		result = 1
	} else {
		result = 0
	}
	return result
}

// Test large array processing
function testLargeArray() {
	const size = 1000
	const arr = []
	let sum = 0
	for (let i = 0; i < size; i = i + 1) {
		arr[i] = i
	}
	for (let j = 0; j < size; j = j + 1) {
		sum = sum + arr[j]
	}
	return sum
}

// Test big computation
function testBigComputation() {
	let result = 0
	for (let i = 0; i < 10000; i = i + 1) {
		result = result + i
	}
	return result
}

// Test JSON operations
function testJson(param) {
	const obj = JSON.parse(param) // NOTE: normally json is evaluated on call, it must be quoted '\'{"name":"test"}\''
	obj.timestamp = Date.now()
	return JSON.stringify(obj)
}

// Test object operations
function testObjectOps() {
	const obj = {
		name: 'QuickJS Test',
		value: 42,
		items: [1, 2, 3]
	}
	return JSON.stringify(obj)
}

// Test string operations
function testStringOps() {
	const str = 'Hello'
	const result = str + ' ' + 'World'
	return result
}

// Parameter type tests
function testParamNumberInt(n) {
	return typeof n + ':' + n
}

function testParamNumberFloat(f) {
	return typeof f + ':' + f
}

function testParamNumberNegative(n) {
	return typeof n + ':' + n
}

function testParamNumberZero(n) {
	return typeof n + ':' + n
}

function testParamString(s) {
	return typeof s + ':' + s
}

function testParamStringEmpty(s) {
	return s.length + ':' + s
}

function testParamStringQuotes(s) {
	return s
}

function testParamBooleanTrue(b) {
	return typeof b + ':' + b
}

function testParamBooleanFalse(b) {
	return typeof b + ':' + b
}

function testParamNull(n) {
	return n === null
}

function testParamTableEmpty(jsonStr) {
	const o = JSON.parse(jsonStr)
	return Object.keys(o).length
}

function testParamTableNested(jsonStr) {
	const o = JSON.parse(jsonStr)
	return o.nested.value
}

function testParamTableArray(jsonStr) {
	const a = JSON.parse(jsonStr)
	return a.length
}

function testParamMixedTypes(num, str, flag, obj) {
	return num + ':' + str + ':' + flag + ':' + obj.x
}

function testParamManyArgs() {
	let total = 0
	for (let i = 0; i < arguments.length; i++) {
		total += arguments[i]
	}
	return total
}

// Console tests
function testConsoleLog() {
	console.log('      quick-js.js log:', 'test message')
	return 'logged'
}

function testConsoleLogNumber() {
	if (!console.debug) {
		console.debug = console.log
		console.debug('      quick-js.js console.debug = console.log')
	}
	console.debug('      quick-js.js debug:', 42)
	return 'logged number'
}

function testConsoleLogMultiple() {
	if (!console.warn) {
		console.warn = console.log
	}
	console.warn('      quick-js.js warn:', 'a', 'b', 'c')
	return 'logged multiple'
}

function testConsoleLogObject() {
	if (!console.error) {
		console.error = console.log
	}
	console.error('      quick-js.js error:', { x: 1, y: 2 })
	return 'logged object'
}

// Export functions to global scope
globalThis.testReturnString = testReturnString
globalThis.testBigParam = testBigParam
globalThis.testInlineCode = testInlineCode
globalThis.testArrayOps = testArrayOps
globalThis.testComplexInline = testComplexInline
globalThis.testNestedCalls = testNestedCalls
globalThis.testConditionals = testConditionals
globalThis.testLargeArray = testLargeArray
globalThis.testBigComputation = testBigComputation
globalThis.testJson = testJson
globalThis.testObjectOps = testObjectOps
globalThis.testStringOps = testStringOps
globalThis.testParamNumberInt = testParamNumberInt
globalThis.testParamNumberFloat = testParamNumberFloat
globalThis.testParamNumberNegative = testParamNumberNegative
globalThis.testParamNumberZero = testParamNumberZero
globalThis.testParamString = testParamString
globalThis.testParamStringEmpty = testParamStringEmpty
globalThis.testParamStringQuotes = testParamStringQuotes
globalThis.testParamBooleanTrue = testParamBooleanTrue
globalThis.testParamBooleanFalse = testParamBooleanFalse
globalThis.testParamNull = testParamNull
globalThis.testParamTableEmpty = testParamTableEmpty
globalThis.testParamTableNested = testParamTableNested
globalThis.testParamTableArray = testParamTableArray
globalThis.testParamMixedTypes = testParamMixedTypes
globalThis.testParamManyArgs = testParamManyArgs
globalThis.testConsoleLog = testConsoleLog
globalThis.testConsoleLogNumber = testConsoleLogNumber
globalThis.testConsoleLogMultiple = testConsoleLogMultiple
globalThis.testConsoleLogObject = testConsoleLogObject
