'use strict';
importScripts('struc_i_v1_0_4_engine.js?v=0.2.2');

self.onmessage = async (event) => {
  const { item, settings, run_id } = event.data;
  const startedAt = new Date().toISOString();
  try {
    const result = await self.STRUC_I_V1_0_4.computeRegisteredProfile(
      item.values,
      settings,
      (fraction, point) => self.postMessage({
        type: 'progress',
        item_id: item.item_id,
        fraction,
        kappa: point.kappa,
        Ak: point.Ak,
        nu: point.nu
      })
    );
    self.postMessage({
      type: 'complete',
      payload: {
        run_id,
        item_id: item.item_id,
        ladder_name: item.ladder_name,
        input_sha256: item.input_sha256,
        engine: 'STRUC-I',
        engine_version: '1.0.4',
        settings,
        started_at: startedAt,
        completed_at: new Date().toISOString(),
        profile: result.profile,
        classification: result.classification
      }
    });
  } catch (error) {
    self.postMessage({ type: 'error', item_id: item.item_id, message: error && error.stack ? error.stack : String(error) });
  }
};
