
const RE_FUNC = /^(?:\(([^)]*?)\))?\s*=>\s*(.*?)\s*$/u;
const RE_BODY = /^\{(.*)\}$/u;

export class Executable extends Function {

  public constructor ( name: string, body: string ) {
    super(...getParams(body));

    Object.defineProperty(this, "name", {
      get ( ): string { return name; },
    });
  }

}

function getParams ( body: string ) {
  const matchFunc = RE_FUNC.exec(body);

  if (!matchFunc) { return [ "", `return(${JSON.stringify(body)})`, ]; }

  const matchBody = RE_BODY.exec(matchFunc[2]!);

  return [ matchFunc[1] ?? "", matchBody?.[1] ?? `return(${matchFunc[2]!})`, ];
}
