'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
var index = require('../../../../hooks/base/useGlobalConfig/index.js');
var index$2 = require('../../../../hooks/base/useElementRemainingHeight/index.js');
var index$1 = require('../../plugins/message/index.js');
var is = require('../../../../utils/src/is.js');
var ruleEditor = require('./ruleEditor.js');

const _hoisted_1 = ["src"];
const ruleEditorEnvKey = "VITE_GIIME_RULE_EDITOR_URL";
var _sfc_main = /* @__PURE__ */ vue.defineComponent({
  ...{
    name: "GmRuleEditor"
  },
  __name: "RuleEditor",
  props: /* @__PURE__ */ vue.mergeModels(ruleEditor.ruleEditorProps, {
    "loaded": { type: Boolean, ...{ default: false } },
    "loadedModifiers": {}
  }),
  emits: /* @__PURE__ */ vue.mergeModels(ruleEditor.ruleEditorEmits, ["update:loaded"]),
  setup(__props, { expose: __expose, emit: __emit }) {
    const props = __props;
    const emit = __emit;
    const importMetaEnv = index.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 = vue.computed(() => {
      let ruleEditorUrl = ``;
      if (is.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 = vue.ref();
    const loaded = vue.useModel(__props, "loaded");
    const init = (data) => {
      if (!loaded.value) return index$1.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;
        }
      }
    };
    vue.onMounted(() => {
      window.addEventListener("message", addEventListener);
    });
    vue.onBeforeUnmount(() => {
      window.removeEventListener("message", addEventListener);
    });
    const GmRuleEditorRef = vue.ref();
    const { height: editorMaxHeight } = index$2.useElementRemainingHeight(GmRuleEditorRef, {
      bottomOffset: props.heightOffset,
      // 高度 - 顶部距离 - 分页器
      minHeight: 300
    });
    __expose({
      init,
      simulate
    });
    return (_ctx, _cache) => {
      return vue.openBlock(), vue.createElementBlock(
        "div",
        {
          ref_key: "GmRuleEditorRef",
          ref: GmRuleEditorRef,
          style: vue.normalizeStyle({ height: _ctx.height ? _ctx.height + "px" : vue.unref(editorMaxHeight) + "px" })
        },
        [
          vue.createElementVNode("iframe", {
            ref_key: "IframeRef",
            ref: IframeRef,
            width: "100%",
            height: "100%",
            src: URL.value,
            frameborder: "0"
          }, null, 8, _hoisted_1)
        ],
        4
        /* STYLE */
      );
    };
  }
});

exports.default = _sfc_main;
//# sourceMappingURL=RuleEditor.vue2.js.map
