import { Direction } from '../geometry/geometry.js';
import { GetExternalLibPath } from '../io/externallibs.js';
import { ConvertThreeGeometryToMesh } from '../threejs/threeutils.js';
import { ImporterBase } from './importerbase.js';

export class ImporterStp extends ImporterBase
{
    constructor ()
    {
        super ();
		this.worker = null;
    }

    CanImportExtension (extension)
    {
        return extension === 'stp' || extension === 'step';
    }

    GetUpDirection ()
    {
        return Direction.Y;
    }

	ClearContent ()
	{

	}

    ResetContent ()
    {

    }

    ImportContent (fileContent, onFinish)
    {
        if (this.worker === null) {
            let workerPath = GetExternalLibPath ('loaders/occt-import-js-worker-cdn.js');
            this.worker = new Worker (workerPath);
        }

        let onModelConverted = (ev) => {
            console.log ('converted');
            this.ImportStepContent (ev.data, onFinish);
            this.worker.removeEventListener ('message', onModelConverted);
        };
        this.worker.addEventListener ('message', onModelConverted);

        let fileBuffer = new Uint8Array (fileContent);
        this.worker.postMessage (fileBuffer);
    }

	ImportStepContent (stepContent, onFinish)
	{
        if (!stepContent.success) {
            return;
        }
        console.log (stepContent);
        for (let occtMesh of stepContent.meshes) {
            let mesh = ConvertThreeGeometryToMesh (occtMesh, null);
            this.model.AddMeshToRootNode (mesh);
        }
        onFinish ();
	}
}

@@ -859,47 +859,8 @@
 %3E %7B%0A
-            console.log ('converted');%0A
     
@@ -907,24 +907,24 @@
  onFinish);%0A
+
             
@@ -997,16 +997,17 @@
      %7D;%0A
+%0A
         
@@ -1065,16 +1065,204 @@
 verted);
+%0A        this.worker.addEventListener ('error', (ev) =%3E %7B%0A            this.worker = null;%0A            this.SetError ('Failed to load occt-import-js.');%0A            onFinish ();%0A        %7D);
 %0A%0A      

Handle error when occt-import-js is not accessible.