<commit_before>import React from "react";
import axios from "axios";
import Editor from "@site/src/components/Editor";
import CodeBlock from "@theme/CodeBlock";

const LiveFeel = ({
  defaultExpression,
  feelContext,
  metadata,
  onResultCallback,
  onErrorCallback,
}) => {
  if (feelContext) {
    // format the context
    feelContext = JSON.stringify(JSON.parse(feelContext), null, 2);
  }

  const [expression, setExpression] = React.useState(defaultExpression);
  const [context, setContext] = React.useState(feelContext);
  const [result, setResult] = React.useState(
    "<click 'Evaluate' to see the result of the expression>"
  );
  const [error, setError] = React.useState(null);

  // https://regex101.com/r/WnWTtz/1
  const errorPattern = /^.+(?<line>\d+):(?<position>\d+).+$/gm;

  // https://regex101.com/r/jus80g/1
  const contextErrorPattern = /^.+at position (?<position>\d+)$/gm;

  const parseContext = () => {
    if (!feelContext) {
      return {};
    }
    return JSON.parse(context);
  };

  function tryEvaluate() {
    try {
      const parsedContext = parseContext();
      evaluate(parsedContext);
    } catch (err) {
      const match = contextErrorPattern.exec(err.message);
      onError({
        message: `failed to parse context: ${err.message}`,
        position: match?.groups?.position,
      });
    }
  }

  function evaluate(parsedContext) {
    axios
      .post(
        "https://feel.upgradingdave.com/process/start",
        {
          expression: expression,
          context: parsedContext,<commit_msg>docs: change to use `feel-service.camunda.com` domain name<commit_after>import React from "react";
import axios from "axios";
import Editor from "@site/src/components/Editor";
import CodeBlock from "@theme/CodeBlock";

const LiveFeel = ({
  defaultExpression,
  feelContext,
  metadata,
  onResultCallback,
  onErrorCallback,
}) => {
  if (feelContext) {
    // format the context
    feelContext = JSON.stringify(JSON.parse(feelContext), null, 2);
  }

  const [expression, setExpression] = React.useState(defaultExpression);
  const [context, setContext] = React.useState(feelContext);
  const [result, setResult] = React.useState(
    "<click 'Evaluate' to see the result of the expression>"
  );
  const [error, setError] = React.useState(null);

  // https://regex101.com/r/WnWTtz/1
  const errorPattern = /^.+(?<line>\d+):(?<position>\d+).+$/gm;

  // https://regex101.com/r/jus80g/1
  const contextErrorPattern = /^.+at position (?<position>\d+)$/gm;

  const parseContext = () => {
    if (!feelContext) {
      return {};
    }
    return JSON.parse(context);
  };

  function tryEvaluate() {
    try {
      const parsedContext = parseContext();
      evaluate(parsedContext);
    } catch (err) {
      const match = contextErrorPattern.exec(err.message);
      onError({
        message: `failed to parse context: ${err.message}`,
        position: match?.groups?.position,
      });
    }
  }

  function evaluate(parsedContext) {
    axios
      .post(
        "https://feel-service.camunda.com/process/start",
        {
          expression: expression,
          context: parsedContext,
{'fraction_non_alphanumeric': 0.1473851030110935, 'fraction_numerical': 0.006973058637083994, 'mean_word_length': 3.158102766798419, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7565448', 'n_tokens_mistral': 1013, 'n_tokens_neox': 967, 'n_words': 213}