import { defineComponent, computed, ref, useModel, onMounted, onBeforeUnmount, createElementBlock, openBlock, normalizeStyle, unref, createElementVNode, mergeModels } from 'vue';
import { useGlobalConfig } from '../../../../hooks/base/useGlobalConfig/index.mjs';
import { useElementRemainingHeight } from '../../../../hooks/base/useElementRemainingHeight/index.mjs';
import { GmMessage } from '../../plugins/message/index.mjs';
import { isString } from '../../../../utils/src/is.mjs';
import { ruleEditorEmits, ruleEditorProps } from './ruleEditor.mjs';

const _hoisted_1 = ["src"];
const ruleEditorEnvKey = "VITE_GIIME_RULE_EDITOR_URL";
var _sfc_main = /* @__PURE__ */ defineComponent({
  ...{
    name: "GmRuleEditor"
  },
  __name: "RuleEditor",
  props: /* @__PURE__ */ mergeModels(ruleEditorProps, {
    "loaded": { type: Boolean, ...{ default: false } },
    "loadedModifiers": {}
  }),
  emits: /* @__PURE__ */ mergeModels(ruleEditorEmits, ["update:loaded"]),
  setup(__props, { expose: __expose, emit: __emit }) {
    const props = __props;
    const emit = __emit;
    const importMetaEnv = useGlobalConfig("env");
    const viteModes = {
      dev: "https://manage-test.giikin.cn/fe/rules-editor",
      develop: "https://manage-test.giikin.cn/fe/rules-editor",
      development: "https://manage-test.giikin.cn/fe/rules-editor",
      test: "https://manage-test.giikin.cn/fe/rules-editor",
      release: "https://manage-test.giikin.cn/fe/rules-editor",
      production: "https://manage.giikin.com/fe/rules-editor"
    };
    const URL = computed(() => {
      let ruleEditorUrl = ``;
      if (isString(importMetaEnv.value[ruleEditorEnvKey])) {
        ruleEditorUrl = importMetaEnv.value[ruleEditorEnvKey];
      } else {
        ruleEditorUrl = viteModes[importMetaEnv.value.MODE] ?? "https://manage.giikin.com/fe/rules-editor";
      }
      return `${ruleEditorUrl}?showClose=${props.showClose}`;
    });
    const IframeRef = ref();
    const loaded = useModel(__props, "loaded");
    const init = (data) => {
      if (!loaded.value) return GmMessage.warning("\u89C4\u5219\u7F16\u8F91\u5668\u5C1A\u672A\u51C6\u5907\u5C31\u7EEA\uFF0C\u8BF7\u7A0D\u540E\u518D\u8BD5\uFF01");
      IframeRef.value?.contentWindow.postMessage(
        {
          action: "details",
          data: JSON.stringify(data)
        },
        "*"
      );
    };
    const simulate = (data) => {
      IframeRef.value?.contentWindow.postMessage(
        {
          action: "simulate",
          data: JSON.stringify(data)
        },
        "*"
      );
    };
    const addEventListener = (e) => {
      const { type, action, data } = e.data;
      const result = JSON.parse(data ?? "{}");
      const { graph, simulate: simulate2 } = result;
      if (type === "rule-editor") {
        switch (action) {
          case "loaded":
            loaded.value = true;
            emit("loaded");
            break;
          case "close":
            emit("close");
            break;
          case "submit":
            emit("submit", { graph, simulate: simulate2 });
            break;
          case "run":
            emit("run", { graph, simulate: simulate2 });
            break;
        }
      }
    };
    onMounted(() => {
      window.addEventListener("message", addEventListener);
    });
    onBeforeUnmount(() => {
      window.removeEventListener("message", addEventListener);
    });
    const GmRuleEditorRef = ref();
    const { height: editorMaxHeight } = useElementRemainingHeight(GmRuleEditorRef, {
      bottomOffset: props.heightOffset,
      // 高度 - 顶部距离 - 分页器
      minHeight: 300
    });
    __expose({
      init,
      simulate
    });
    return (_ctx, _cache) => {
      return openBlock(), createElementBlock(
        "div",
        {
          ref_key: "GmRuleEditorRef",
          ref: GmRuleEditorRef,
          style: normalizeStyle({ height: _ctx.height ? _ctx.height + "px" : unref(editorMaxHeight) + "px" })
        },
        [
          createElementVNode("iframe", {
            ref_key: "IframeRef",
            ref: IframeRef,
            width: "100%",
            height: "100%",
            src: URL.value,
            frameborder: "0"
          }, null, 8, _hoisted_1)
        ],
        4
        /* STYLE */
      );
    };
  }
});

export { _sfc_main as default };
//# sourceMappingURL=RuleEditor.vue2.mjs.map
