// lib/javascript/porffor/libporffor.js
// plugin/vue-renderer/renderer/porffor/libporffor.js
// libporffor.js - Single file for Porffor AOT compilation
// ES6 imports require bundling with esbuild first

import { importTest } from './test-es6-import.js'

// Test: return simple string
function testReturnString() {
	const ret = 'hello world ' + (Date.now() % 1000)
	console.warn('    console.warn call from libporffor.js' + ret)
	return ret
}

// Main render function - returns BIG JSON > 100KB
function testBigParam(paramStr) {
	return 'libporffor: ' + (typeof paramStr === 'string' ? paramStr.length + ' ' : '') + paramStr
	/* JSON.stringify and JSON.parse is not supported yet in porffor
	const rows = 50
	const bigArray = []
	for (let i = 0; i < rows; i = i + 1) {
		bigArray.push({
			id: i,
			name: 'Item ' + i,
			value: i * 100
		})
	} */
	/* return JSON.stringify({
		param: paramStr || '',
		count: rows,
		items: bigArray,
		timestamp: Date.now()
	}) */
}

// 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
}

function test(callFunc) {
	// Export functions
	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.importTest = importTest
	if (callFunc) {
		// Call to prevent tree-shaking
		testReturnString()
		testBigParam('hello')
		testInlineCode()
		testArrayOps()
		testComplexInline()
		testNestedCalls()
		testConditionals()
		testLargeArray()
		testBigComputation()
		importTest()
	}
}
test(false)
