/** @jsxImportSource preact */
import '@shopify/ui-extensions/preact';
import {render} from 'preact';
import {RecsBlock, numericGid} from './shared.jsx';

// Thank you page: the purchased items are right there in shopify.lines, pass
// them as seeds so the serve merges their per-product precomputed lists.
export default function extension(){
  render(<Extension />, document.body);
}

function Extension(){
  const lines = (shopify.lines && shopify.lines.value) || [];
  const ids = [];
  for (const line of lines) {
    const pid = numericGid(line && line.merchandise && line.merchandise.product && line.merchandise.product.id);
    if (pid && ids.indexOf(pid) === -1) ids.push(pid);
  }
  return <RecsBlock params={ids.length ? {product_ids: ids.join(',')} : {}} />;
}
