wasm := "_build/wasm/release/build/impl/impl.wasm"
wasm_fixed := "_build/wasm/release/build/impl/impl.fixed.wasm"
out := "pixelmatch-component.wasm"

default:
    @just --list

generate:
    cd {{justfile_directory()}} && moon-component generate wit --out-dir . --no-impl

build-wasm:
    cd {{justfile_directory()}} && moon build --target wasm --release

# Fix MoonBit WASM codegen bug (FixedArray.op_set stack issue)
fix-wasm: build-wasm
    cd {{justfile_directory()}} && python3 scripts/fix-wasm-codegen.py {{wasm}} {{wasm_fixed}}

# Build WASM component (embed WIT + create component)
build: fix-wasm
    cd {{justfile_directory()}} && wasm-tools component embed wit {{wasm_fixed}} -o _build/impl.embedded.wasm
    cd {{justfile_directory()}} && wasm-tools component new _build/impl.embedded.wasm -o {{out}}

transpile: build
    npx jco transpile {{out}} -o test/gen

test: transpile
    node test/test.mjs

wit: build
    wasm-tools component wit {{out}}

clean:
    rm -rf _build {{out}} test/gen
