import { computed as g, onMounted as Ye, ref as b, watch as De, createElementBlock as c, openBlock as i, unref as r, normalizeStyle as x, normalizeClass as ye, createBlock as F, createCommentVNode as k, createSlots as Ge, withCtx as w, renderSlot as m, normalizeProps as R, guardReactiveProps as X, createVNode as be, createElementVNode as M, Fragment as I, renderList as A, toDisplayString as W, createTextVNode as Ue, nextTick as Ve } from "vue";
import { u as Be, o as je, e as Re, c as ke, t as Xe, p as q, a as We, b as qe, v as h, f as we, X as Ze, G as Ce, L as Je, i as Ke, q as Qe, r as et } from "./index-CTpA678x.js";
import { _ as tt } from "./Title-Dp1KpSLT.js";
import { u as ot, U as lt } from "./usePrinter-WPNAmU6x.js";
import { D as at } from "./DataTable-fKXm7dWw.js";
import nt from "./vue-ui-accordion-BSjdTzOb.js";
import st from "./vue-ui-skeleton-CU4p-C4n.js";
import { u as $e } from "./useNestedProp-B95uADZw.js";
import { _ as rt } from "./PackageVersion-DqLLMNcj.js";
import { P as ut } from "./PenAndPaper-b7rqpk80.js";
import { u as it } from "./useUserOptionState-BIvW1Kz7.js";
import { u as ct } from "./useChartAccessibility-BWojgys7.js";
import { _ as dt } from "./_plugin-vue_export-helper-CHgC5LLL.js";
const vt = ["id"], ht = {
  key: 1,
  ref: "noTitle",
  class: "vue-data-ui-no-title-space",
  style: "height:36px; width: 100%;background:transparent"
}, ft = {
  key: 2,
  style: "width:100%;background:transparent;padding-bottom:24px"
}, gt = ["xmlns", "viewBox"], pt = ["width", "height"], mt = ["id"], yt = ["stop-color"], bt = ["stop-color"], kt = ["d", "fill", "stroke", "stroke-width"], wt = ["x", "y", "height", "width", "fill", "stroke", "stroke-width", "onMouseenter"], Ct = ["x", "y", "font-size", "fill"], $t = ["x", "y", "font-size", "fill"], xt = {
  key: 4,
  class: "vue-data-ui-watermark"
}, _t = {
  key: 6,
  ref: "source",
  dir: "auto"
}, Nt = ["innerHTML"], Tt = {
  __name: "vue-ui-flow",
  props: {
    config: {
      type: Object,
      default() {
        return {};
      }
    },
    dataset: {
      type: Array,
      default() {
        return [];
      }
    }
  },
  setup(xe, { expose: _e }) {
    const { vue_ui_flow: Ne } = Be(), p = xe, z = g(() => !!p.dataset && p.dataset.length);
    Ye(() => {
      Z();
    });
    function Z() {
      je(p.dataset) && Re({
        componentName: "VueUiFlow",
        type: "dataset"
      });
    }
    const Y = b(ke()), J = b(null), K = b(0), Q = b(0), T = b(!1);
    function Te(t) {
      T.value = t, K.value += 1;
    }
    const e = g({
      get: () => le(),
      set: (t) => t
    }), { userOptionsVisible: D, setUserOptionsVisibility: ee, keepUserOptionState: te } = it({ config: e.value }), { svgRef: oe } = ct({ config: e.value.style.chart.title });
    function le() {
      const t = $e({
        userConfig: p.config,
        defaultConfig: Ne
      });
      return t.theme ? {
        ...$e({
          userConfig: We.vue_ui_flow[t.theme] || p.config,
          defaultConfig: t
        }),
        customPalette: Xe[t.theme] || q
      } : t;
    }
    De(() => p.config, (t) => {
      e.value = le(), D.value = !e.value.userOptions.showOnChartHover, Z(), Q.value += 1, O.value.showTable = e.value.table.show;
    }, { deep: !0 });
    const { isPrinting: ae, isImaging: ne, generatePdf: se, generateImage: re } = ot({
      elementId: `flow_${Y.value}`,
      fileName: e.value.style.chart.title.text || "vue-ui-flow"
    }), Oe = g(() => e.value.userOptions.show && !e.value.style.chart.title.text), G = g(() => qe(e.value.customPalette)), ue = g(() => e.value.style.chart.nodes.gap), _ = g(() => e.value.style.chart.nodes.width), O = b({
      showTable: e.value.table.show
    }), ie = g(() => e.value.style.chart.links.width), U = g(() => !p.dataset || !p.dataset.length ? [] : p.dataset.map((t) => [
      t[0],
      t[1],
      h(t[2])
    ])), ce = g(() => {
      const t = {};
      function l(n) {
        t[n] || (t[n] = { inflow: 0, outflow: 0 });
      }
      U.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 Pe(t) {
      return t / ce.value * 100 + e.value.style.chart.nodes.minHeight;
    }
    function Fe(t) {
      const l = e.value.style.chart.nodes.minHeight;
      return (t - l) / 100 * ce.value;
    }
    function Ie(t) {
      const l = {}, o = {};
      function n(s, u) {
        l[s] || (l[s] = { height: 0, level: null, inflow: 0, outflow: 0 }), l[s].level === null && (l[s].level = u), o[u] || (o[u] = []), o[u].includes(s) || o[u].push(s);
      }
      t.forEach(([s, u, f], N) => {
        const $ = l[s] ? l[s].level : 0, E = $ + 1;
        n(s, $), n(u, E), l[s].children || (l[s].children = []), l[s].children.push({ target: u, value: f }), l[s].outflow += f, l[u].inflow += f;
      }), Object.keys(l).forEach((s, u) => {
        l[s].color = G.value[u] || G.value[u % G.value.length] || q[u] || q[u % d.length];
      });
      for (const s in l)
        l[s].height = Pe(Math.max(l[s].inflow, l[s].outflow)), l[s].name = s;
      const a = {};
      for (const s in o) {
        let u = 0;
        o[s].forEach((f, N) => {
          const $ = l[f].height;
          a[f] = {
            x: parseInt(s, 10) * ie.value + e.value.style.chart.padding.left,
            y: u,
            absoluteY: u,
            height: $,
            i: N,
            color: l[f].color,
            value: Fe($)
          }, u += $ + ue.value;
        });
      }
      const v = [];
      for (const s in l) {
        let u = 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, B = a[s], j = a[f], fe = h(u), ge = h(u + N / l[s].outflow * B.height), pe = h(E), me = h(E + N / l[f].inflow * j.height), ze = {
            id: ke(),
            source: s,
            target: f,
            path: `M ${h(B.x) + h(_.value)} ${fe} L ${h(B.x) + h(_.value)} ${ge} L ${h(j.x)} ${me} L ${h(j.x)} ${pe} Z`,
            value: N,
            sourceColor: l[s].color,
            targetColor: l[f].color
          };
          v.push(ze), u += ge - fe, a[f].y += me - pe;
        });
      }
      return { nodeCoordinates: a, links: v };
    }
    const y = g(() => {
      const t = Ie(p.dataset);
      return {
        nodes: Object.keys(t.nodeCoordinates).map((l, o) => ({
          ...t.nodeCoordinates[l],
          name: l
        })),
        links: t.links
      };
    }), Ae = g(() => 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 + ue.value;
      }
      return Math.max(...Object.values(l));
    }
    const P = g(() => {
      const { top: t, right: l, left: o, bottom: n } = e.value.style.chart.padding, a = U.value.length * ie.value;
      return {
        height: Ae.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 He(t) {
      const l = {}, o = {}, n = /* @__PURE__ */ new Set();
      return U.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), L = b(null);
    function Se(t) {
      C.value = He(t.name), L.value = t.name;
    }
    function Ee() {
      C.value = null, L.value = null;
    }
    const de = g(() => 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 ve() {
      Ve(() => {
        const t = de.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 = Qe(l);
        et({
          csvContent: o,
          title: e.value.style.chart.title.text || "vue-ui-flow"
        });
      });
    }
    const H = g(() => {
      const t = [
        e.value.table.columnNames.source,
        e.value.table.columnNames.target,
        e.value.table.columnNames.value
      ], l = de.value.map((a, v) => [
        {
          color: a.sourceColor,
          name: a.source
        },
        {
          color: a.targetColor,
          name: a.target
        },
        we({
          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 Me() {
      return y.value;
    }
    function he() {
      O.value.showTable = !O.value.showTable;
    }
    const S = b(!1);
    function V() {
      S.value = !S.value;
    }
    return _e({
      getData: Me,
      generateCsv: ve,
      generateImage: re,
      generatePdf: se,
      toggleTable: he,
      toggleAnnotator: V
    }), (t, l) => (i(), c("div", {
      ref_key: "flowChart",
      ref: J,
      class: ye(`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] = () => r(ee)(!0)),
      onMouseleave: l[3] || (l[3] = () => r(ee)(!1))
    }, [
      e.value.userOptions.buttons.annotator ? (i(), F(ut, {
        key: 0,
        svgRef: r(oe),
        backgroundColor: e.value.style.chart.backgroundColor,
        color: e.value.style.chart.color,
        active: S.value,
        onClose: V
      }, null, 8, ["svgRef", "backgroundColor", "color", "active"])) : k("", !0),
      Oe.value ? (i(), c("div", ht, null, 512)) : k("", !0),
      e.value.style.chart.title.text ? (i(), c("div", ft, [
        (i(), F(tt, {
          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 && (r(te) || r(D)) ? (i(), F(lt, {
        ref: "details",
        key: `user_option_${K.value}`,
        backgroundColor: e.value.style.chart.backgroundColor,
        color: e.value.style.chart.color,
        isPrinting: r(ae),
        isImaging: r(ne),
        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: J.value,
        position: e.value.userOptions.position,
        hasAnnotator: e.value.userOptions.buttons.annotator,
        isAnnotation: S.value,
        onToggleFullscreen: Te,
        onGeneratePdf: r(se),
        onGenerateCsv: ve,
        onGenerateImage: r(re),
        onToggleTable: he,
        onToggleAnnotator: V,
        style: x({
          visibility: r(te) ? r(D) ? "visible" : "hidden" : "visible"
        })
      }, Ge({ _: 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", R(X({ 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", R(X({ 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),
      (i(), c("svg", {
        ref_key: "svgRef",
        ref: oe,
        xmlns: r(Ze),
        viewBox: `0 0 ${P.value.width} ${P.value.height}`,
        class: ye({ "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}`)
      }, [
        be(rt),
        t.$slots["chart-background"] ? (i(), 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, pt)) : k("", !0),
        M("defs", null, [
          (i(!0), c(I, null, A(y.value.links, (o, n) => (i(), c("linearGradient", {
            id: o.id,
            x1: "0%",
            y1: "0%",
            x2: "100%",
            y2: "0%"
          }, [
            M("stop", {
              offset: "0%",
              "stop-color": o.sourceColor
            }, null, 8, yt),
            M("stop", {
              offset: "100%",
              "stop-color": o.targetColor
            }, null, 8, bt)
          ], 8, mt))), 256))
        ]),
        (i(!0), c(I, null, A(y.value.links, (o) => (i(), 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:${L.value ? [o.target, o.source].includes(L.value) ? 1 : 0.3 : e.value.style.chart.links.opacity}`)
        }, null, 12, kt))), 256)),
        (i(!0), c(I, null, A(y.value.nodes, (o, n) => (i(), c("rect", {
          class: "vue-ui-flow-node",
          x: o.x,
          y: r(h)(o.absoluteY) + e.value.style.chart.padding.top,
          height: r(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) => Se(o),
          onMouseleave: l[0] || (l[0] = (a) => Ee()),
          style: x(`opacity:${C.value ? C.value.includes(o.name) ? 1 : 0.2 : 1}`)
        }, null, 44, wt))), 256)),
        (i(!0), c(I, null, A(y.value.nodes, (o, n) => (i(), c("text", {
          x: o.x + _.value / 2,
          y: r(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: r(Ce)(o.color),
          "text-anchor": "middle",
          style: x(`pointer-events: none; opacity:${C.value ? C.value.includes(o.name) ? 1 : 0 : 1}`)
        }, W(e.value.style.chart.nodes.labels.abbreviation.use ? r(Je)({ source: o.name, length: e.value.style.chart.nodes.labels.abbreviation.length }) : o.name), 13, Ct))), 256)),
        (i(!0), c(I, null, A(y.value.nodes, (o, n) => (i(), c("text", {
          x: o.x + _.value / 2,
          y: r(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: r(Ce)(o.color),
          "text-anchor": "middle",
          style: x(`pointer-events: none; opacity:${C.value ? C.value.includes(o.name) ? 1 : 0 : 1}`)
        }, W(r(Ke)(
          e.value.style.chart.nodes.labels.formatter,
          o.value,
          r(we)({
            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, $t))), 256)),
        m(t.$slots, "svg", { svg: P.value }, void 0, !0)
      ], 14, gt)),
      t.$slots.watermark ? (i(), c("div", xt, [
        m(t.$slots, "watermark", R(X({ isPrinting: r(ae) || r(ne) })), void 0, !0)
      ])) : k("", !0),
      z.value ? k("", !0) : (i(), F(st, {
        key: 5,
        config: {
          type: "flow",
          style: {
            backgroundColor: e.value.style.chart.backgroundColor
          }
        }
      }, null, 8, ["config"])),
      t.$slots.source ? (i(), c("div", _t, [
        m(t.$slots, "source", {}, void 0, !0)
      ], 512)) : k("", !0),
      z.value ? (i(), F(nt, {
        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(() => [
          be(at, {
            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, Nt)
            ]),
            td: w(({ td: o }) => [
              Ue(W(o.name || o), 1)
            ]),
            _: 1
          }, 8, ["colNames", "head", "body", "config", "title"])
        ]),
        _: 1
      }, 8, ["config"])) : k("", !0)
    ], 46, vt));
  }
}, Gt = /* @__PURE__ */ dt(Tt, [["__scopeId", "data-v-9a2880b1"]]);
export {
  Gt as default
};
