import { computed as p, onMounted as ze, ref as b, watch as Ye, openBlock as u, createElementBlock as c, normalizeClass as me, normalizeStyle as x, unref as i, createBlock as F, createCommentVNode as k, createSlots as De, withCtx as w, renderSlot as m, normalizeProps as W, guardReactiveProps as q, createVNode as ye, createElementVNode as M, Fragment as I, renderList as L, toDisplayString as R, createTextVNode as Ge, nextTick as Ue } from "vue";
import { u as Ve, o as Be, e as je, c as be, t as Xe, a as We, p as Z, b as qe, v as h, f as ke, X as Re, G as we, L as Ze, i as Je, q as Ke, r as Qe } from "./index-CxkSLqkR.js";
import { _ as et } from "./Title-DlWf9Ddn.js";
import { u as tt, U as ot } from "./usePrinter-C6iBjau1.js";
import { D as lt } from "./DataTable-BE81z-1L.js";
import at from "./vue-ui-accordion-ElsMWD7y.js";
import nt from "./vue-ui-skeleton-BNcyfNtG.js";
import { u as Ce } from "./useNestedProp-Cl0RLl8U.js";
import { _ as st } from "./PackageVersion-CZWTAkjP.js";
import { P as rt } from "./PenAndPaper-B-Xy37LR.js";
import { u as ut } from "./useUserOptionState-BIvW1Kz7.js";
import { _ as it } from "./_plugin-vue_export-helper-CHgC5LLL.js";
const ct = ["id"], dt = {
  key: 1,
  ref: "noTitle",
  class: "vue-data-ui-no-title-space",
  style: "height:36px; width: 100%;background:transparent"
}, vt = {
  key: 2,
  style: "width:100%;background:transparent;padding-bottom:24px"
}, ht = ["xmlns", "viewBox"], ft = ["width", "height"], pt = ["id"], gt = ["stop-color"], mt = ["stop-color"], yt = ["d", "fill", "stroke", "stroke-width"], bt = ["x", "y", "height", "width", "fill", "stroke", "stroke-width", "onMouseenter"], kt = ["x", "y", "font-size", "fill"], wt = ["x", "y", "font-size", "fill"], Ct = {
  key: 4,
  class: "vue-data-ui-watermark"
}, $t = {
  key: 6,
  ref: "source",
  dir: "auto"
}, xt = ["innerHTML"], _t = {
  __name: "vue-ui-flow",
  props: {
    config: {
      type: Object,
      default() {
        return {};
      }
    },
    dataset: {
      type: Array,
      default() {
        return [];
      }
    }
  },
  setup($e, { expose: xe }) {
    const g = $e, { vue_ui_flow: _e } = Ve(), z = p(() => !!g.dataset && g.dataset.length);
    ze(() => {
      J();
    });
    function J() {
      Be(g.dataset) && je({
        componentName: "VueUiFlow",
        type: "dataset"
      });
    }
    const Y = b(be()), D = b(null), K = b(0), Q = b(0), T = b(!1);
    function Ne(t) {
      T.value = t, K.value += 1;
    }
    const e = p({
      get: () => oe(),
      set: (t) => t
    }), { userOptionsVisible: G, setUserOptionsVisibility: ee, keepUserOptionState: te } = ut({ config: e.value });
    function oe() {
      const t = Ce({
        userConfig: g.config,
        defaultConfig: _e
      });
      return t.theme ? {
        ...Ce({
          userConfig: Xe.vue_ui_flow[t.theme] || g.config,
          defaultConfig: t
        }),
        customPalette: We[t.theme] || Z
      } : t;
    }
    Ye(() => g.config, (t) => {
      e.value = oe(), G.value = !e.value.userOptions.showOnChartHover, J(), Q.value += 1, O.value.showTable = e.value.table.show;
    }, { deep: !0 });
    const { isPrinting: le, isImaging: ae, generatePdf: ne, generateImage: se } = tt({
      elementId: `flow_${Y.value}`,
      fileName: e.value.style.chart.title.text || "vue-ui-flow"
    }), Te = p(() => e.value.userOptions.show && !e.value.style.chart.title.text), U = p(() => qe(e.value.customPalette)), re = p(() => e.value.style.chart.nodes.gap), _ = p(() => e.value.style.chart.nodes.width), O = b({
      showTable: e.value.table.show
    }), ue = p(() => e.value.style.chart.links.width), V = p(() => !g.dataset || !g.dataset.length ? [] : g.dataset.map((t) => [
      t[0],
      t[1],
      h(t[2])
    ])), ie = p(() => {
      const t = {};
      function l(n) {
        t[n] || (t[n] = { inflow: 0, outflow: 0 });
      }
      V.value.forEach(([n, a, v]) => {
        l(n), l(a), t[n].outflow += v, t[a].inflow += v;
      });
      let o = 0;
      for (const n in t) {
        const a = Math.max(t[n].inflow, t[n].outflow);
        o = Math.max(o, a);
      }
      return o;
    });
    function Oe(t) {
      return t / ie.value * 100 + e.value.style.chart.nodes.minHeight;
    }
    function Pe(t) {
      const l = e.value.style.chart.nodes.minHeight;
      return (t - l) / 100 * ie.value;
    }
    function Fe(t) {
      const l = {}, o = {};
      function n(s, r) {
        l[s] || (l[s] = { height: 0, level: null, inflow: 0, outflow: 0 }), l[s].level === null && (l[s].level = r), o[r] || (o[r] = []), o[r].includes(s) || o[r].push(s);
      }
      t.forEach(([s, r, f], N) => {
        const $ = l[s] ? l[s].level : 0, E = $ + 1;
        n(s, $), n(r, E), l[s].children || (l[s].children = []), l[s].children.push({ target: r, value: f }), l[s].outflow += f, l[r].inflow += f;
      }), Object.keys(l).forEach((s, r) => {
        l[s].color = U.value[r] || U.value[r % U.value.length] || Z[r] || Z[r % d.length];
      });
      for (const s in l)
        l[s].height = Oe(Math.max(l[s].inflow, l[s].outflow)), l[s].name = s;
      const a = {};
      for (const s in o) {
        let r = 0;
        o[s].forEach((f, N) => {
          const $ = l[f].height;
          a[f] = {
            x: parseInt(s, 10) * ue.value + e.value.style.chart.padding.left,
            y: r,
            absoluteY: r,
            height: $,
            i: N,
            color: l[f].color,
            value: Pe($)
          }, r += $ + re.value;
        });
      }
      const v = [];
      for (const s in l) {
        let r = a[s].absoluteY + e.value.style.chart.padding.top;
        l[s].children && l[s].children.forEach(({ target: f, value: N }, $) => {
          const E = a[f].y + e.value.style.chart.padding.top, j = a[s], X = a[f], he = h(r), fe = h(r + N / l[s].outflow * j.height), pe = h(E), ge = h(E + N / l[f].inflow * X.height), Me = {
            id: be(),
            source: s,
            target: f,
            path: `M ${h(j.x) + h(_.value)} ${he} L ${h(j.x) + h(_.value)} ${fe} L ${h(X.x)} ${ge} L ${h(X.x)} ${pe} Z`,
            value: N,
            sourceColor: l[s].color,
            targetColor: l[f].color
          };
          v.push(Me), r += fe - he, a[f].y += ge - pe;
        });
      }
      return { nodeCoordinates: a, links: v };
    }
    const y = p(() => {
      const t = Fe(g.dataset);
      return {
        nodes: Object.keys(t.nodeCoordinates).map((l, o) => ({
          ...t.nodeCoordinates[l],
          name: l
        })),
        links: t.links
      };
    }), Ie = p(() => Le(y.value.nodes));
    function Le(t) {
      const l = {};
      for (const n in t) {
        const { x: a, height: v } = t[n];
        l[a] || (l[a] = 0), l[a] += v + re.value;
      }
      return Math.max(...Object.values(l));
    }
    const P = p(() => {
      const { top: t, right: l, left: o, bottom: n } = e.value.style.chart.padding, a = V.value.length * ue.value;
      return {
        height: Ie.value + t + n,
        width: l + Math.max(...y.value.nodes.map((v) => v.x)) + _.value,
        left: o,
        top: t,
        right: a - l,
        p_top: t,
        p_bottom: n
      };
    });
    function Ae(t) {
      const l = {}, o = {}, n = /* @__PURE__ */ new Set();
      return V.value.forEach(([a, v, s]) => {
        l[a] || (l[a] = []), o[v] || (o[v] = []), l[a].push(v), o[v].push(a);
      }), l[t] && l[t].forEach((a) => n.add(a)), o[t] && o[t].forEach((a) => n.add(a)), Array.from(n).concat(t);
    }
    const C = b(null), A = b(null);
    function He(t) {
      C.value = Ae(t.name), A.value = t.name;
    }
    function Se() {
      C.value = null, A.value = null;
    }
    const ce = p(() => y.value.links.map(({ source: t, target: l, sourceColor: o, targetColor: n, value: a }) => ({
      source: t,
      target: l,
      sourceColor: o,
      targetColor: n,
      value: a
    })));
    function de() {
      Ue(() => {
        const t = ce.value.map((n, a) => [
          [n.source],
          [n.target],
          [n.value]
        ]), l = [
          [e.value.style.chart.title.text],
          [e.value.style.chart.title.subtitle.text],
          [
            [e.value.table.columnNames.source],
            [e.value.table.columnNames.target],
            [e.value.table.columnNames.value]
          ]
        ].concat(t), o = Ke(l);
        Qe({
          csvContent: o,
          title: e.value.style.chart.title.text || "vue-ui-flow"
        });
      });
    }
    const H = p(() => {
      const t = [
        e.value.table.columnNames.source,
        e.value.table.columnNames.target,
        e.value.table.columnNames.value
      ], l = ce.value.map((a, v) => [
        {
          color: a.sourceColor,
          name: a.source
        },
        {
          color: a.targetColor,
          name: a.target
        },
        ke({
          p: e.value.style.chart.nodes.labels.prefix,
          v: a.value,
          s: e.value.style.chart.nodes.labels.suffix,
          r: e.value.style.chart.nodes.labels.rounding
        })
      ]), o = {
        th: {
          backgroundColor: e.value.table.th.backgroundColor,
          color: e.value.table.th.color,
          outline: e.value.table.th.outline
        },
        td: {
          backgroundColor: e.value.table.td.backgroundColor,
          color: e.value.table.td.color,
          outline: e.value.table.td.outline
        },
        breakpoint: e.value.table.responsiveBreakpoint
      };
      return {
        colNames: [
          e.value.table.columnNames.source,
          e.value.table.columnNames.target,
          e.value.table.columnNames.value
        ],
        head: t,
        body: l,
        config: o
      };
    });
    function Ee() {
      return y.value;
    }
    function ve() {
      O.value.showTable = !O.value.showTable;
    }
    const S = b(!1);
    function B() {
      S.value = !S.value;
    }
    return xe({
      getData: Ee,
      generateCsv: de,
      generateImage: se,
      generatePdf: ne,
      toggleTable: ve,
      toggleAnnotator: B
    }), (t, l) => (u(), c("div", {
      ref_key: "flowChart",
      ref: D,
      class: me(`vue-ui-flow ${T.value ? "vue-data-ui-wrapper-fullscreen" : ""}`),
      style: x(`font-family:${e.value.style.fontFamily};width:100%; text-align:center;background:${e.value.style.chart.backgroundColor}`),
      id: `flow_${Y.value}`,
      onMouseenter: l[2] || (l[2] = () => i(ee)(!0)),
      onMouseleave: l[3] || (l[3] = () => i(ee)(!1))
    }, [
      e.value.userOptions.buttons.annotator ? (u(), F(rt, {
        key: 0,
        parent: D.value,
        backgroundColor: e.value.style.chart.backgroundColor,
        color: e.value.style.chart.color,
        active: S.value,
        onClose: B
      }, null, 8, ["parent", "backgroundColor", "color", "active"])) : k("", !0),
      Te.value ? (u(), c("div", dt, null, 512)) : k("", !0),
      e.value.style.chart.title.text ? (u(), c("div", vt, [
        (u(), F(et, {
          key: `title_${Q.value}`,
          config: {
            title: {
              cy: "flow-title",
              ...e.value.style.chart.title
            },
            subtitle: {
              cy: "flow-subtitle",
              ...e.value.style.chart.title.subtitle
            }
          }
        }, null, 8, ["config"]))
      ])) : k("", !0),
      e.value.userOptions.show && z.value && (i(te) || i(G)) ? (u(), F(ot, {
        ref: "details",
        key: `user_option_${K.value}`,
        backgroundColor: e.value.style.chart.backgroundColor,
        color: e.value.style.chart.color,
        isPrinting: i(le),
        isImaging: i(ae),
        uid: Y.value,
        hasPdf: e.value.userOptions.buttons.pdf,
        hasXls: e.value.userOptions.buttons.csv,
        hasImg: e.value.userOptions.buttons.img,
        hasTable: e.value.userOptions.buttons.table,
        hasFullscreen: e.value.userOptions.buttons.fullscreen,
        isFullscreen: T.value,
        titles: { ...e.value.userOptions.buttonTitles },
        chartElement: D.value,
        position: e.value.userOptions.position,
        hasAnnotator: e.value.userOptions.buttons.annotator,
        isAnnotation: S.value,
        onToggleFullscreen: Ne,
        onGeneratePdf: i(ne),
        onGenerateCsv: de,
        onGenerateImage: i(se),
        onToggleTable: ve,
        onToggleAnnotator: B,
        style: x({
          visibility: i(te) ? i(G) ? "visible" : "hidden" : "visible"
        })
      }, De({ _: 2 }, [
        t.$slots.optionPdf ? {
          name: "optionPdf",
          fn: w(() => [
            m(t.$slots, "optionPdf", {}, void 0, !0)
          ]),
          key: "0"
        } : void 0,
        t.$slots.optionCsv ? {
          name: "optionCsv",
          fn: w(() => [
            m(t.$slots, "optionCsv", {}, void 0, !0)
          ]),
          key: "1"
        } : void 0,
        t.$slots.optionImg ? {
          name: "optionImg",
          fn: w(() => [
            m(t.$slots, "optionImg", {}, void 0, !0)
          ]),
          key: "2"
        } : void 0,
        t.$slots.optionTable ? {
          name: "optionTable",
          fn: w(() => [
            m(t.$slots, "optionTable", {}, void 0, !0)
          ]),
          key: "3"
        } : void 0,
        t.$slots.optionFullscreen ? {
          name: "optionFullscreen",
          fn: w(({ toggleFullscreen: o, isFullscreen: n }) => [
            m(t.$slots, "optionFullscreen", W(q({ toggleFullscreen: o, isFullscreen: n })), void 0, !0)
          ]),
          key: "4"
        } : void 0,
        t.$slots.optionAnnotator ? {
          name: "optionAnnotator",
          fn: w(({ toggleAnnotator: o, isAnnotator: n }) => [
            m(t.$slots, "optionAnnotator", W(q({ toggleAnnotator: o, isAnnotator: n })), void 0, !0)
          ]),
          key: "5"
        } : void 0
      ]), 1032, ["backgroundColor", "color", "isPrinting", "isImaging", "uid", "hasPdf", "hasXls", "hasImg", "hasTable", "hasFullscreen", "isFullscreen", "titles", "chartElement", "position", "hasAnnotator", "isAnnotation", "onGeneratePdf", "onGenerateImage", "style"])) : k("", !0),
      (u(), c("svg", {
        xmlns: i(Re),
        viewBox: `0 0 ${P.value.width} ${P.value.height}`,
        class: me({ "vue-data-ui-fullscreen--on": T.value, "vue-data-ui-fulscreen--off": !T.value }),
        style: x(`max-width:100%; overflow: visible; background:transparent;color:${e.value.style.chart.color}`)
      }, [
        ye(st),
        t.$slots["chart-background"] ? (u(), c("foreignObject", {
          key: 0,
          x: 0,
          y: 0,
          width: P.value.width,
          height: P.value.height,
          style: {
            pointerEvents: "none"
          }
        }, [
          m(t.$slots, "chart-background", {}, void 0, !0)
        ], 8, ft)) : k("", !0),
        M("defs", null, [
          (u(!0), c(I, null, L(y.value.links, (o, n) => (u(), c("linearGradient", {
            id: o.id,
            x1: "0%",
            y1: "0%",
            x2: "100%",
            y2: "0%"
          }, [
            M("stop", {
              offset: "0%",
              "stop-color": o.sourceColor
            }, null, 8, gt),
            M("stop", {
              offset: "100%",
              "stop-color": o.targetColor
            }, null, 8, mt)
          ], 8, pt))), 256))
        ]),
        (u(!0), c(I, null, L(y.value.links, (o) => (u(), c("path", {
          class: "vue-ui-flow-link",
          d: o.path,
          fill: `url(#${o.id})`,
          stroke: e.value.style.chart.links.stroke,
          "stroke-width": e.value.style.chart.links.strokeWidth,
          style: x(`opacity:${A.value ? [o.target, o.source].includes(A.value) ? 1 : 0.3 : e.value.style.chart.links.opacity}`)
        }, null, 12, yt))), 256)),
        (u(!0), c(I, null, L(y.value.nodes, (o, n) => (u(), c("rect", {
          class: "vue-ui-flow-node",
          x: o.x,
          y: i(h)(o.absoluteY) + e.value.style.chart.padding.top,
          height: i(h)(o.height),
          width: _.value,
          fill: o.color,
          stroke: e.value.style.chart.nodes.stroke,
          "stroke-width": e.value.style.chart.nodes.strokeWidth,
          onMouseenter: (a) => He(o),
          onMouseleave: l[0] || (l[0] = (a) => Se()),
          style: x(`opacity:${C.value ? C.value.includes(o.name) ? 1 : 0.2 : 1}`)
        }, null, 44, bt))), 256)),
        (u(!0), c(I, null, L(y.value.nodes, (o, n) => (u(), c("text", {
          x: o.x + _.value / 2,
          y: i(h)(o.absoluteY + o.height / 2 - e.value.style.chart.nodes.labels.fontSize / 4) + e.value.style.chart.padding.top,
          "font-size": e.value.style.chart.nodes.labels.fontSize,
          fill: i(we)(o.color),
          "text-anchor": "middle",
          style: x(`pointer-events: none; opacity:${C.value ? C.value.includes(o.name) ? 1 : 0 : 1}`)
        }, R(e.value.style.chart.nodes.labels.abbreviation.use ? i(Ze)({ source: o.name, length: e.value.style.chart.nodes.labels.abbreviation.length }) : o.name), 13, kt))), 256)),
        (u(!0), c(I, null, L(y.value.nodes, (o, n) => (u(), c("text", {
          x: o.x + _.value / 2,
          y: i(h)(o.absoluteY + o.height / 2 + e.value.style.chart.nodes.labels.fontSize) + e.value.style.chart.padding.top,
          "font-size": e.value.style.chart.nodes.labels.fontSize,
          fill: i(we)(o.color),
          "text-anchor": "middle",
          style: x(`pointer-events: none; opacity:${C.value ? C.value.includes(o.name) ? 1 : 0 : 1}`)
        }, R(i(Je)(
          e.value.style.chart.nodes.labels.formatter,
          o.value,
          i(ke)({
            p: e.value.style.chart.nodes.labels.prefix,
            v: o.value,
            s: e.value.style.chart.nodes.labels.suffix,
            r: e.value.style.chart.nodes.labels.rounding
          }),
          { datapoint: o, seriesIndex: n }
        )), 13, wt))), 256)),
        m(t.$slots, "svg", { svg: P.value }, void 0, !0)
      ], 14, ht)),
      t.$slots.watermark ? (u(), c("div", Ct, [
        m(t.$slots, "watermark", W(q({ isPrinting: i(le) || i(ae) })), void 0, !0)
      ])) : k("", !0),
      z.value ? k("", !0) : (u(), F(nt, {
        key: 5,
        config: {
          type: "flow",
          style: {
            backgroundColor: e.value.style.chart.backgroundColor
          }
        }
      }, null, 8, ["config"])),
      t.$slots.source ? (u(), c("div", $t, [
        m(t.$slots, "source", {}, void 0, !0)
      ], 512)) : k("", !0),
      z.value ? (u(), F(at, {
        key: 7,
        hideDetails: "",
        config: {
          open: O.value.showTable,
          maxHeight: 1e4,
          body: {
            backgroundColor: e.value.style.chart.backgroundColor,
            color: e.value.style.chart.color
          },
          head: {
            backgroundColor: e.value.style.chart.backgroundColor,
            color: e.value.style.chart.color
          }
        }
      }, {
        content: w(() => [
          ye(lt, {
            colNames: H.value.colNames,
            head: H.value.head,
            body: H.value.body,
            config: H.value.config,
            title: `${e.value.style.chart.title.text}${e.value.style.chart.title.subtitle.text ? ` : ${e.value.style.chart.title.subtitle.text}` : ""}`,
            onClose: l[1] || (l[1] = (o) => O.value.showTable = !1)
          }, {
            th: w(({ th: o }) => [
              M("div", {
                innerHTML: o,
                style: { display: "flex", "align-items": "center" }
              }, null, 8, xt)
            ]),
            td: w(({ td: o }) => [
              Ge(R(o.name || o), 1)
            ]),
            _: 1
          }, 8, ["colNames", "head", "body", "config", "title"])
        ]),
        _: 1
      }, 8, ["config"])) : k("", !0)
    ], 46, ct));
  }
}, zt = /* @__PURE__ */ it(_t, [["__scopeId", "data-v-0aa84563"]]);
export {
  zt as default
};
