import { FC, ReactNode, useEffect, useState } from "react";
import { useGlobal, useLocal } from "web-utils";
import { IMeta } from "../../ed/logic/ed-global";
import { ViGlobal } from "./global";
import { ViChild } from "./render";
import { viEvalProps } from "./script/eval-prop";
import { viEvalScript } from "./script/eval-script";

export const ViScript: FC<{
  meta: IMeta;
  children: ReactNode;
  passprop?: any;
}> = ({ meta, children, passprop }) => {
  const vi = useGlobal(ViGlobal, "VI");
  const [_, _set] = useState({});
  meta.render = () => {
    _set({});
  };

  let _pass = passprop;
  if (meta.item.component?.id) {
    if (!_pass) _pass = {};
    viEvalProps(vi, meta, _pass);
  }

  if (meta.item.adv?.js) {
    viEvalScript(vi, meta, _pass);
    if (meta.script) return meta.script.result;
  }

  return (
    <ViChild meta={meta} passprop={_pass}>
      {children}
    </ViChild>
  );
};
