import { defineComponent as $, ref as b, resolveComponent as s, openBlock as w, createBlock as D, withCtx as n, createElementVNode as B, createVNode as t, createTextVNode as c, inject as N, unref as S, createCommentVNode as h, isRef as R, computed as O, createElementBlock as U, Fragment as j } from "vue";
import { E as A, d as F, h as M, i as T } from "./index-a68a4fdc.mjs";
const z = { class: "dialog-footer" }, I = /* @__PURE__ */ $({
  __name: "DataEditorDialog",
  setup(E, { expose: V }) {
    function y(g, o) {
      u.value = g, v = o, r.value = !0;
    }
    const r = b(!1), u = b({});
    let v;
    const i = b(), _ = async (g) => {
      g && await g.validate((o) => {
        o && (r.value = !1, v && v(u.value));
      });
    };
    return V({ show: y }), (g, o) => {
      const x = s("el-input"), f = s("el-form-item"), a = s("el-option"), l = s("el-select"), e = s("el-form"), m = s("el-button"), C = s("el-dialog");
      return w(), D(C, {
        modelValue: r.value,
        "onUpdate:modelValue": o[5] || (o[5] = (d) => r.value = d),
        title: "Data editor",
        "close-on-click-modal": !1,
        "close-on-press-escape": !1
      }, {
        footer: n(() => [
          B("span", z, [
            t(m, {
              onClick: o[3] || (o[3] = (d) => r.value = !1)
            }, {
              default: n(() => [
                c("Cancel")
              ]),
              _: 1
            }),
            t(m, {
              type: "primary",
              onClick: o[4] || (o[4] = (d) => _(i.value))
            }, {
              default: n(() => [
                c(" Save ")
              ]),
              _: 1
            })
          ])
        ]),
        default: n(() => [
          t(e, {
            ref_key: "dataEditorFormRef",
            ref: i,
            model: u.value,
            "label-width": "120px"
          }, {
            default: n(() => [
              t(f, {
                label: "Key",
                prop: "key",
                required: ""
              }, {
                default: n(() => [
                  t(x, {
                    modelValue: u.value.key,
                    "onUpdate:modelValue": o[0] || (o[0] = (d) => u.value.key = d)
                  }, null, 8, ["modelValue"])
                ]),
                _: 1
              }),
              t(f, {
                label: "Description",
                prop: "description"
              }, {
                default: n(() => [
                  t(x, {
                    modelValue: u.value.description,
                    "onUpdate:modelValue": o[1] || (o[1] = (d) => u.value.description = d)
                  }, null, 8, ["modelValue"])
                ]),
                _: 1
              }),
              t(f, {
                label: "Type",
                prop: "type",
                required: ""
              }, {
                default: n(() => [
                  t(l, {
                    modelValue: u.value.type,
                    "onUpdate:modelValue": o[2] || (o[2] = (d) => u.value.type = d)
                  }, {
                    default: n(() => [
                      t(a, {
                        label: "String",
                        value: "String"
                      }),
                      t(a, {
                        label: "Number",
                        value: "Number"
                      }),
                      t(a, {
                        label: "Boolean",
                        value: "Boolean"
                      }),
                      t(a, {
                        label: "Object",
                        value: "Object"
                      }),
                      t(a, {
                        label: "Array",
                        value: "Array"
                      })
                    ]),
                    _: 1
                  }, 8, ["modelValue"])
                ]),
                _: 1
              })
            ]),
            _: 1
          }, 8, ["model"])
        ]),
        _: 1
      }, 8, ["modelValue"]);
    };
  }
}), J = { class: "dialog-footer" }, q = /* @__PURE__ */ $({
  __name: "DataValueDialog",
  setup(E, { expose: V }) {
    const y = N("context"), r = b(), u = b(!0), v = b(!1);
    let i, _ = 0;
    function g(l, e) {
      i = l, _ = e, r.value = x(l), o.value = !0;
    }
    const o = b(!1);
    function x(l) {
      let e;
      return _ == 0 ? e = l.initValue : (e = y.dataManager.get(i.key), e = R(e) ? e.value : e), e != null ? l.type == "String" ? e : JSON.stringify(e, null, 2) : l.type == "Object" ? "{}" : l.type == "Array" ? "[]" : l.type == "String" ? "" : l.type == "Number" ? "0" : l.type == "Boolean" ? "true" : "";
    }
    function f() {
      return i.type == "Object" || i.type == "Array" ? JSON.parse(r.value) : i.type == "String" ? r.value : i.type == "Number" ? parseFloat(r.value) : i.type == "Boolean" ? r.value == "true" : "";
    }
    function a() {
      let l;
      try {
        l = f();
      } catch (e) {
        A({
          title: "Error",
          message: e.message,
          type: "error"
        });
        return;
      }
      _ == 0 ? (i.initValue = l, u.value && y.dataManager.set(i.key, l)) : (y.dataManager.set(i.key, l), v.value && (i.initValue = l)), o.value = !1;
    }
    return V({ show: g }), (l, e) => {
      const m = s("b-ace-editor"), C = s("el-switch"), d = s("el-button"), k = s("el-dialog");
      return w(), D(k, {
        modelValue: o.value,
        "onUpdate:modelValue": e[4] || (e[4] = (p) => o.value = p),
        title: "Data value"
      }, {
        footer: n(() => [
          B("span", J, [
            S(_) == 0 ? (w(), D(C, {
              key: 0,
              style: { "margin-right": "20px" },
              modelValue: u.value,
              "onUpdate:modelValue": e[1] || (e[1] = (p) => u.value = p),
              size: "large",
              "inactive-text": "Reset current value to init value while saving"
            }, null, 8, ["modelValue"])) : h("", !0),
            S(_) == 1 ? (w(), D(C, {
              key: 1,
              style: { "margin-right": "20px" },
              modelValue: v.value,
              "onUpdate:modelValue": e[2] || (e[2] = (p) => v.value = p),
              size: "large",
              "inactive-text": "Reset init value to current value while saving"
            }, null, 8, ["modelValue"])) : h("", !0),
            t(d, {
              onClick: e[3] || (e[3] = (p) => o.value = !1)
            }, {
              default: n(() => [
                c("Cancel")
              ]),
              _: 1
            }),
            t(d, {
              type: "primary",
              onClick: a
            }, {
              default: n(() => [
                c("Save")
              ]),
              _: 1
            })
          ])
        ]),
        default: n(() => [
          t(m, {
            modelValue: r.value,
            "onUpdate:modelValue": e[0] || (e[0] = (p) => r.value = p),
            lang: "json",
            width: "100%",
            height: "50vh",
            readonly: !1,
            "font-size": 14
          }, null, 8, ["modelValue"])
        ]),
        _: 1
      }, 8, ["modelValue"]);
    };
  }
}), P = /* @__PURE__ */ $({
  __name: "index",
  setup(E) {
    const V = b(), y = b();
    let r = -1;
    const u = N("context");
    function v() {
      r = -1, V.value.show({}, _);
    }
    function i(a) {
      r = a.$index;
      const l = F(a.row);
      V.value.show(l, _);
    }
    const _ = (a) => {
      if (r < 0) {
        for (const l of f.value)
          if (l.key == a.key) {
            M.error("Data key [" + a.key + "] is already existed");
            return;
          }
        f.value.push(a);
      } else
        f.value[r] = a;
    }, g = (a) => {
      T.confirm(
        "Will you want to delte this data",
        "Warning",
        {
          confirmButtonText: "Yes",
          cancelButtonText: "No",
          type: "warning"
        }
      ).then(() => {
        f.value.splice(a.$index, 1), u.dataManager.clear(a.row.key);
      });
    }, o = (a) => {
      y.value.show(a.row, 0);
    }, x = (a) => {
      y.value.show(a.row, 1);
    }, f = O(() => u.codeManager.getCode().data);
    return (a, l) => {
      const e = s("el-table-column"), m = s("el-button"), C = s("el-button-group"), d = s("el-table");
      return w(), U(j, null, [
        t(d, {
          data: f.value,
          border: ""
        }, {
          empty: n(() => [
            c(" Please "),
            t(m, {
              type: "primary",
              onClick: l[1] || (l[1] = (k) => v())
            }, {
              default: n(() => [
                c("Add")
              ]),
              _: 1
            }),
            c(" a new record ")
          ]),
          default: n(() => [
            t(e, {
              prop: "key",
              label: "Key"
            }),
            t(e, {
              prop: "description",
              label: "Description"
            }),
            t(e, {
              prop: "type",
              label: "Type"
            }),
            t(e, {
              fixed: "right",
              label: "Operations",
              width: "420px"
            }, {
              default: n((k) => [
                t(C, null, {
                  default: n(() => [
                    t(m, {
                      type: "primary",
                      onClick: l[0] || (l[0] = (p) => v())
                    }, {
                      default: n(() => [
                        c("Add")
                      ]),
                      _: 1
                    }),
                    t(m, {
                      type: "success",
                      onClick: (p) => i(k)
                    }, {
                      default: n(() => [
                        c("Edit")
                      ]),
                      _: 2
                    }, 1032, ["onClick"]),
                    t(m, {
                      type: "danger",
                      onClick: (p) => g(k)
                    }, {
                      default: n(() => [
                        c("Delete")
                      ]),
                      _: 2
                    }, 1032, ["onClick"]),
                    t(m, {
                      type: "primary",
                      onClick: (p) => o(k)
                    }, {
                      default: n(() => [
                        c("Init value")
                      ]),
                      _: 2
                    }, 1032, ["onClick"]),
                    t(m, {
                      type: "success",
                      onClick: (p) => x(k)
                    }, {
                      default: n(() => [
                        c("Current value")
                      ]),
                      _: 2
                    }, 1032, ["onClick"])
                  ]),
                  _: 2
                }, 1024)
              ]),
              _: 1
            })
          ]),
          _: 1
        }, 8, ["data"]),
        t(I, {
          ref_key: "dataEditorDialogRef",
          ref: V
        }, null, 512),
        t(q, {
          ref_key: "dataValueDialogRef",
          ref: y
        }, null, 512)
      ], 64);
    };
  }
});
export {
  P as default
};
