// src/framework/shadowlisp/primitives/SlList.ts
//
// Vision 49: List constructor element - IS a Matrix component
//
// <sl-list><a><b><c></sl-list> => [a, b, c]

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

/**
 * SlList - List constructor element that IS a Matrix component.
 *
 * Evaluates all children and returns them as a list.
 *
 * @example
 * <sl-list>
 *   <sl-num>1</sl-num>
 *   <sl-num>2</sl-num>
 *   <sl-num>3</sl-num>
 * </sl-list>
 * => [1, 2, 3]
 */
export class SlList extends SlMatrixElement {
  async evalSelf(env: SExpr[]): Promise<SExpr> {
    const results: SExpr[] = [];

    for (const child of this.children) {
      const value = await this.evalChild(child, env);
      results.push(value);
    }

    return results;
  }

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

if (!customElements.get('sl-list')) {
  customElements.define('sl-list', SlList);
}
