<script setup>
import { Effect } from "effect";
import { cel as _rawCel } from "../../../../../utils/cel";
import { celBindings, injectCELContext } from "../../../../../utils/cel-context";
import { dispatchTriggers, useEventChannel } from "../../../../../share/event-bus";
import ShwfedActionDefinition from "../../../components/definition.vue";
import { executeHttpRequest } from "../../../utils/resolve";
defineOptions({ name: "ShwfedHttpRequestActionRuntime" });
const props = defineProps({
  buttonId: { type: String, required: true },
  config: { type: null, required: true }
});
const inherited = injectCELContext();
const $cel = (expression, context) => _rawCel(expression, { ...celBindings(inherited), ...context });
const channel = useEventChannel();
const effect = Effect.suspend(
  () => Effect.andThen(
    dispatchTriggers(channel, props.config.before, $cel),
    () => executeHttpRequest(props.config.expression, $cel, {
      messageExpression: props.config.messageExpression,
      resultExpression: props.config.resultExpression,
      channel,
      triggers: {
        success: props.config.onSuccess,
        warning: props.config.onWarning,
        error: props.config.onError,
        info: props.config.onInfo
      }
    })
  )
);
</script>

<template>
  <ShwfedActionDefinition
    :action-id="buttonId"
    :effect="effect"
  />
</template>
