<script setup>
import { Effect } from "effect";
import { useI18n } from "vue-i18n";
import { cel as _rawCel } from "../../../../../utils/cel";
import { reject } from "../../../../../composables/useOverlay";
import { celBindings, injectCELContext } from "../../../../../utils/cel-context";
import { getLocalizedText } from "../../../../../share/locale";
import { dispatchTriggers, useEventChannel } from "../../../../../share/event-bus";
import ShwfedActionDefinition from "../../../components/definition.vue";
import { executeHttpRequest, interpolateMarkdown } from "../../../utils/resolve";
defineOptions({ name: "ShwfedHttpRequestConfirmActionRuntime" });
const props = defineProps({
  buttonId: { type: String, required: true },
  config: { type: null, required: true }
});
const { locale } = useI18n();
const inherited = injectCELContext();
const $cel = (expression, context) => _rawCel(expression, { ...celBindings(inherited), ...context });
const channel = useEventChannel();
const effect = Effect.gen(function* () {
  yield* dispatchTriggers(channel, props.config.before);
  const raw = getLocalizedText(props.config.markdown, locale.value) ?? "";
  const content = interpolateMarkdown(raw, $cel);
  if (yield* reject({ content, icon: props.config.icon, color: props.config.color })) return;
  yield* 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>
