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