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