import { ref as v, computed as c } from "vue";
import { u as p } from "./netsuite-BGUUAOqe.js";
import { u as g, A as h, a as E, E as b } from "./app-Bpb4394e.js";
import { useSnackbar as I } from "@feedmepos/ui-library";
import { u as m } from "./feature-Cz6F0_cm.js";
function C() {
  return {
    setting: {
      _id: "",
      enable: !1,
      bindings: [],
      uniqueCode: "",
      branchCode: "",
      server: {
        password: "",
        host: "",
        username: "",
        port: "",
        directory: ""
      },
      histories: [],
      modifiers: [],
      exportAsJson: !1
    },
    menu: [],
    group: [],
    variants: [],
    categories: []
  };
}
const i = v(C());
function w() {
  return {
    // state
    get isEnabled() {
      return i.value.setting.enable;
    },
    get isBusinessLocation() {
      return g().businessLevel;
    },
    get state() {
      return i.value;
    },
    async init() {
      var e, t, n, d;
      const o = h(), a = E();
      try {
        const r = await a.readCachedMenu();
        i.value.menu = ((e = r.modules) == null ? void 0 : e.item) ?? [], i.value.group = ((t = r.modules) == null ? void 0 : t.group) ?? [], i.value.variants = ((n = r.modules) == null ? void 0 : n.variant) ?? [];
        const f = (((d = r.modules) == null ? void 0 : d.category) ?? []).map((s) => ({
          ...s,
          _id: s._id === void 0 ? null : s._id
        }));
        i.value.categories = f, i.value.setting = await o.readSetting();
      } catch (r) {
        I().open({
          title: "Something went wrong",
          message: "Cannot fetch integration info. Please try again.",
          type: "error"
        }), console.error("Error at fetching integration info", r);
      }
    }
  };
}
function S() {
  return {
    locationId: ""
  };
}
const l = v(S());
function x() {
  const o = g(), a = b();
  return o.watchLocation(async (e) => {
    if (m().enabledXilnexOpeningMigration) {
      if (!e) {
        l.value = S();
        return;
      }
      l.value.locationId = e.dbName;
      const n = await a.readLastClosing(e.dbName);
      n ? l.value.closingDocumentId = n._id : l.value.closingDocumentId = "";
    }
  }), {
    get isEnabled() {
      const e = m();
      return c(() => e.enabledXilnexOpeningMigration).value;
    },
    get state() {
      return l.value;
    },
    async fetchClosingDocumentId() {
      const e = g(), t = await a.readLastClosing(e.currentLocationDBName);
      t ? l.value.closingDocumentId = t._id : l.value.closingDocumentId = "";
    }
  };
}
function y() {
  return {
    locationId: ""
  };
}
const u = v(y());
function D() {
  const o = g(), a = b();
  return o.watchLocation(async (e) => {
    if (m()) {
      if (!e) {
        u.value = y();
        return;
      }
      u.value.locationId = e.dbName;
      const n = await a.readLastClosing(e.dbName);
      n ? u.value.closingDocumentId = n._id : u.value.closingDocumentId = "";
    }
  }), {
    get isEnabled() {
      const e = m();
      return c(() => e.enabledSynergyOpeningMigration).value;
    },
    get state() {
      return u.value;
    },
    async fetchClosingDocumentId() {
      const e = g(), t = await a.readLastClosing(e.currentLocationDBName);
      t ? u.value.closingDocumentId = t._id : u.value.closingDocumentId = "";
    }
  };
}
function A() {
  const o = p(), a = w(), e = x(), t = D(), n = c(() => o.isEnabled), d = c(() => a.isEnabled), r = c(() => e.isEnabled), f = c(() => t.isEnabled);
  return {
    get enabledIntegrations() {
      return [
        n.value,
        d.value,
        r.value,
        f.value
      ].filter((s) => s).length;
    },
    get isNetSuiteEnabled() {
      return n.value;
    },
    async fetchFoodMarketHubSetting() {
      try {
        await a.init();
      } catch (s) {
        console.error("Error fetching FoodMarketHub setting:", s);
      }
    },
    async fetchNetSuiteSetting() {
      try {
        await o.readSetting();
      } catch (s) {
        console.error("Error fetching NetSuite setting:", s);
      }
    }
  };
}
export {
  x as a,
  D as b,
  A as c,
  w as u
};
