var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => {
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
  return value;
};
import e, { useState as t, useRef as o, useImperativeHandle as s, useEffect as n, lazy as i, Suspense as r, useMemo as a } from "react";
import { l, s as p, R as d, u } from "./comps-f4e34e6a.js";
import { n as c, P as h, R as m, o as g } from "./react-d37f274e.js";
import { j as f, a as w, T as I, C as b, d as v, G as y } from "./common-64096f9e.js";
import { a as C } from "./axios-e064bed6.js";
import { A as D, r as j, k as M, l as U, s as T } from "./constants-6ebafe5b.js";
import { l as E } from "./lodash-62e10097.js";
import { L as O } from "./styled-components-f8105b39.js";
import { s as V } from "./pages-06cd5d02.js";
import { F as L, A as S } from "./antd-960523cf.js";
import { D as _ } from "./api-9408842b.js";
import { be as A } from "./vendor-208d0ff5.js";
import { r as P } from "./app_redux-c7ca7a7f.js";
import { E as R } from "./util-31a5c3fb.js";
async function k(e2, t2, o2 = {}) {
  if (!t2)
    return void console.error("node must be not null.");
  const { AppViewInstance: s2 } = await Promise.resolve().then(() => G);
  return new s2(e2, t2, c(t2), o2);
}
l();
const $ = e.forwardRef(function(e2, i2) {
  const { appId: r2, className: a2, onModuleEventTriggered: l2, onModuleOutputChange: p2, ...d2 } = e2, [u2, c2] = t(), h2 = o(null);
  return s(i2, () => u2, [u2]), n(() => {
    const e3 = h2.current;
    e3 && k(r2, e3, d2).then(c2);
  }, [r2]), n(() => {
    u2 && (u2.on("moduleOutputChange", p2), u2.on("moduleEventTriggered", l2));
  }, [u2, l2, p2]), n(() => {
    d2.moduleInputs && u2 && u2.setModuleInputs(d2.moduleInputs);
  }, [d2.moduleInputs, u2]), f("div", { ref: h2, className: a2 });
}), x = i(() => Promise.resolve().then(() => B).then((e2) => ({ default: e2.AppView })));
let z = false;
class N {
  constructor(e2, t2, o2, s2 = {}) {
    __publicField(this, "comp", null);
    __publicField(this, "prevOutputs", null);
    __publicField(this, "events", /* @__PURE__ */ new Map());
    __publicField(this, "dataPromise");
    __publicField(this, "options", { baseUrl: "https://api-service.lowcoder.cloud", webUrl: "https://app.lowcoder.cloud" });
    __publicField(this, "authorizedUser", true);
    this.appId = e2, this.node = t2, this.root = o2, Object.assign(this.options, s2), this.options.baseUrl && (T.baseURL = this.options.baseUrl), this.options.webUrl && (T.webUrl = this.options.webUrl), this.dataPromise = this.loadData(), this.render();
  }
  isModuleDSL(e2) {
    var _a;
    return "module" === ((_a = e2 == null ? void 0 : e2.ui) == null ? void 0 : _a.compType);
  }
  async loadData() {
    var _a, _b, _c;
    const { baseUrl: e2, appDsl: t2, moduleDslMap: o2, webUrl: s2 } = this.options;
    let n2 = t2, i2 = o2;
    if (p({ isViewMode: true, applicationId: this.appId }), !t2) {
      const t3 = C.create({ baseURL: e2, withCredentials: true }), o3 = await t3.get(`/api/applications/${this.appId}/view`).then((e3) => e3.data).catch((e3) => {
        var _a2;
        if (((_a2 = e3.response) == null ? void 0 : _a2.status) === D.REQUEST_NOT_AUTHORISED)
          return V({ [M.redirectUrl]: encodeURIComponent(window.location.href), [M.loginType]: null }), this.authorizedUser = false, { data: { orgCommonSettings: void 0, applicationDSL: {}, moduleDSL: {} } };
      });
      await _.fetchJsDatasourceByApp(this.appId).then((e3) => {
        var _a2, _b2;
        (_b2 = (_a2 = e3.data) == null ? void 0 : _a2.data) == null ? void 0 : _b2.forEach((e4) => {
          j(e4.type, e4.id, e4.pluginDefinition);
        });
      }), p({ orgCommonSettings: (_a = o3 == null ? void 0 : o3.data) == null ? void 0 : _a.orgCommonSettings }), n2 = ((_b = o3 == null ? void 0 : o3.data) == null ? void 0 : _b.applicationDSL) || {}, i2 = ((_c = o3 == null ? void 0 : o3.data) == null ? void 0 : _c.moduleDSL) || {};
    }
    if (this.options.moduleInputs && this.isModuleDSL(n2)) {
      const e3 = "ui.comp.io.inputs", t3 = E.get(n2, e3, []).map((e4) => {
        const t4 = this.options.moduleInputs[e4.name];
        return t4 ? { ...e4, defaultValue: { ...e4.defaultValue, comp: t4 } } : e4;
      });
      E.set(n2, e3, t3);
    }
    return { appDsl: n2, moduleDslMap: i2 };
  }
  handleCompChange(e2) {
    if (this.comp = e2, !e2)
      return;
    const t2 = e2.children.ui.getModuleLayoutComp();
    if (t2) {
      const e3 = t2.children.io.children.outputs.getView(), o2 = Object.fromEntries(e3.map((e4) => {
        const { name: t3, value: o3 } = e4.getView();
        return [t3, o3];
      }));
      E.isEqual(this.prevOutputs, o2) || (this.prevOutputs = o2, this.emit("moduleOutputChange", [o2]));
    }
  }
  async render() {
    const e2 = await this.dataPromise, t2 = this.options.orgId ? `${this.options.webUrl}/org/${this.options.orgId}/auth/login` : `${this.options.webUrl}${U}`;
    this.root.render(this.authorizedUser ? f(O, { target: this.node, children: f(r, { fallback: null, children: f(x, { appId: this.appId, dsl: e2.appDsl, moduleDsl: e2.moduleDslMap, moduleInputs: this.options.moduleInputs, onCompChange: (e3) => this.handleCompChange(e3), onModuleEventTriggered: (e3) => this.emit("moduleEventTriggered", [e3]) }) }) }) : w(L, { vertical: true, align: "center", justify: "center", children: [f("h4", { children: "This resource needs authentication." }), f(I, z ? { buttonType: "primary", onClick: () => {
      window.location.reload();
    }, children: "Refresh" } : { buttonType: "primary", onClick: () => {
      z = true, window.open(t2, "_blank"), this.render();
    }, children: "Login" })] }));
  }
  emit(e2, t2) {
    const o2 = this.events.get(e2);
    o2 && o2.apply(this, t2);
  }
  on(e2, t2) {
    return t2 ? (this.events.set(e2, t2), () => this.events.delete(e2)) : () => {
    };
  }
  setModuleInputs(e2) {
    this.options.moduleInputs = e2, this.render();
  }
  invokeMethod(e2, t2 = []) {
    if (!this.comp)
      return;
    const o2 = this.comp.children.ui.getModuleLayoutComp();
    o2 && o2.children.methods.executeMethodByName(e2, t2);
  }
}
const G = Object.freeze(Object.defineProperty({ __proto__: null, AppViewInstance: N }, Symbol.toStringTag, { value: "Module" })), H = A();
const B = Object.freeze(Object.defineProperty({ __proto__: null, AppView: function(e2) {
  const { dsl: t2, moduleDsl: s2, appId: i2, moduleInputs: r2, onCompChange: l2, onModuleEventTriggered: p2 } = e2, c2 = o(p2);
  c2.current = p2;
  const I2 = a(() => ({ Comp: d, initialValue: t2, reduceContext: { readOnly: true, applicationId: i2, parentApplicationPath: [], moduleDSL: s2 || {} }, initHandler: (e3) => e3.preload(`app-${i2}`), actionPreInterceptor: (e3) => {
    var _a;
    return e3.type !== b.TRIGGER_MODULE_EVENT || ((_a = c2.current) == null ? void 0 : _a.call(c2, e3.name), false);
  } }), [i2]), [C2] = u(I2);
  return n(() => {
    l2 == null ? void 0 : l2(C2);
  }, [C2]), n(() => {
    const e3 = C2 == null ? void 0 : C2.children.ui.getModuleLayoutComp();
    if (!e3 || !r2)
      return;
    e3.children.io.children.inputs.getView().forEach((e4) => {
      const { name: t3 } = e4.getView(), o2 = r2[t3];
      void 0 !== o2 && e4.children.defaultValue.children.comp.dispatch(v(e4.children.defaultValue.children.comp.changeValueAction(o2)));
    });
  }, [r2]), w(S, { children: [f(y, {}), f(h, { store: P, children: f(R.Provider, { value: { applicationId: i2, appType: 1, readOnly: true, hideHeader: true }, children: f(m, { history: H, children: f(g, { path: "/", render: () => C2 == null ? void 0 : C2.getView() }) }) }) })] });
} }, Symbol.toStringTag, { value: "Module" }));
export {
  N as A,
  $ as L,
  k as b
};
