// src/framework/shadowlisp/primitives/SlLt.ts
//
// Vision 49: Less-than element - IS a Matrix component
//
// <sl-lt><a><b></sl-lt> => a < b

import type { SExpr } from '../../../lisp/SExpr';
import { SlMatrixElement } from '../SlMatrixElement';
import { SlBootstrap } from '../SlBootstrap';

/**
 * SlLt - Less-than comparison element that IS a Matrix component.
 *
 * @example
 * <sl-lt><sl-num>3</sl-num><sl-num>5</sl-num></sl-lt>  => true
 */
export class SlLt extends SlMatrixElement {
  async evalSelf(env: SExpr[]): Promise<SExpr> {
    const children = Array.from(this.children);
    if (children.length < 2) {
      throw new Error('sl-lt requires 2 children');
    }

    const a = await this.evalChild(children[0], env) as number;
    const b = await this.evalChild(children[1], env) as number;

    return SlBootstrap.lt(a, b);
  }

  toSExpr(): SExpr {
    const args = Array.from(this.children).map(c =>
      (c instanceof SlMatrixElement) ? c.toSExpr() : null
    );
    return ['<', ...args];
  }
}

if (!customElements.get('sl-lt')) {
  customElements.define('sl-lt', SlLt);
}
