"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.init = void 0;

var Shopify = _interopRequireWildcard(require("./shopify"));

var Firebase = _interopRequireWildcard(require("./firebase"));

var Config = _interopRequireWildcard(require("./config"));

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

const init = async (data, app) => {
  var _data$query, _data$params;

  const shop = (data === null || data === void 0 ? void 0 : (_data$query = data.query) === null || _data$query === void 0 ? void 0 : _data$query.shop) || (data === null || data === void 0 ? void 0 : (_data$params = data.params) === null || _data$params === void 0 ? void 0 : _data$params.shop) || (data === null || data === void 0 ? void 0 : data.get("x-shopify-shop-domain"));
  console.log(`[APP] Initialising app for ${shop}`);
  const config = Config.getConfig(app);
  const admin = await Firebase.getFirebase(config);
  const db = await Firebase.getFirebaseRef(config, admin, shop); // TODO: if shop is not found then throw error

  const settings = await Firebase.getFirebaseSettings(db);
  const token = await Firebase.getFirebaseShopifyToken(db);
  const shopify = await Shopify.getShopifyApi(config, shop, token);
  console.log(`[APP] Successfully initialised config, admin, db, settings and shopify`);
  return {
    shop,
    config,
    admin,
    db,
    settings,
    shopify
  };
};

exports.init = init;
//# sourceMappingURL=app.js.map