'use strict';

const index = require('./index-4901314f.js');
const getHandlebarsHelpers = require('./getHandlebarsHelpers-8ef28401.js');
const localforage = require('./localforage-af1b8c0e.js');

const config = {
  env: "live",
  url: "https://theflood.team",
  algolia: {
    appId: "ZKFP93O6EH",
    searchKey: "cef2b52143823594c9c5af78ad86bcf0",
  },
  dade: {
    apiKey: "668112a786ef9e357d5dbb53993afa62",
    url: "https://lockbox.dadesystems.com/api/billers/670",
  },
  functions: {
    url: "https://us-central1-the-flood-team.cloudfunctions.net",
  },
  firebase: {
    apiKey: "AIzaSyCMZzl6TZViOFx3G3Wz7omxL56e_WnMAzQ",
    authDomain: "the-flood-team.firebaseapp.com",
    databaseURL: "https://the-flood-team-default-rtdb.firebaseio.com",
    projectId: "the-flood-team",
    storageBucket: "the-flood-team.appspot.com",
    messagingSenderId: "1065444417180",
    appId: "1:1065444417180:web:cd5fd2a1a6b15cf2f0d759",
    measurementId: "G-8J4NCLDTBN",
  },
  graphql: {
    url: "https://theflood.team/graphql",
  },
  logrocketKey: "mfh3ua/flood-team-app",
  stripe: {
    key: "pk_live_r5b0hheVLZcWxxyUS0cNkTlC",
    clientId: "ca_DRtszUtwgrKfRt9XkV5ArzRJYKLJV39m",
  },
  dynamicLinkDomain: "floodteam.page.link",
  franchise: {
    stripe: {
      key: "pk_live_B1NjOFNTbjMmRcPd6AeLrnDG00wukem0hk",
      clientId: "ca_H3DErpg90RI5wuL2mZU3AMjp1DumT2k0",
    },
    dade: {
      apiKey: "a4f3f7e624de9fded4af23a40b3f0ba4",
      url: "https://lockbox.dadesystems.com/api/billers/966",
    },
  },
};

function env(key, fallback) {
  const value = !key
    ? config
    : key && key.indexOf(".") >= 0
      ? key.split(".").reduce((p, c) => (p && p[c]) || null, config)
      : key && typeof config[key] !== "undefined"
        ? config[key]
        : null;
  return fallback &&
    ((!value && value !== false && value !== 0) ||
      typeof value === "undefined" ||
      value === null)
    ? fallback
    : value;
}

/**
 * Cleans all JavaScript referneces and prototypes from Object
 * so it can be stored in Firestore.
 * @param input The JS Object to clean
 * @param keepDocumentReferenceId Should we keep reference ids for relationships
 */
async function cleanObjectOfReferences(input, keepDocumentReferenceId = false) {
  var _a, _b, _c, _d, _e;
  const data = input;
  for (const key of Object.keys(input)) {
    const value = input[key];
    if (!value)
      continue;
    try {
      if (((_a = value === null || value === void 0 ? void 0 : value.constructor) === null || _a === void 0 ? void 0 : _a.name) === "Object") {
        data[key] = await cleanObjectOfReferences(value);
      }
      else if (((_b = value === null || value === void 0 ? void 0 : value.constructor) === null || _b === void 0 ? void 0 : _b.name) === "DocumentReference") {
        keepDocumentReferenceId
          ? (data[key] = { id: value.id })
          : delete data[key];
      }
      else if (((_c = value === null || value === void 0 ? void 0 : value.constructor) === null || _c === void 0 ? void 0 : _c.name) === "Timestamp") {
        data[key] = value.toDate();
      }
      else if (((_d = value === null || value === void 0 ? void 0 : value.constructor) === null || _d === void 0 ? void 0 : _d.name) === "Array") {
        const cleanArray = [];
        for (const item of data[key]) {
          cleanArray.push(await cleanObjectOfReferences(item));
        }
        data[key] = cleanArray;
      }
      else if (typeof value === "object" &&
        ((_e = value === null || value === void 0 ? void 0 : value.constructor) === null || _e === void 0 ? void 0 : _e.name) !== "Date") {
        data[key] = await cleanObjectOfReferences(JSON.parse(JSON.stringify(value)));
      }
    }
    catch (err) {
      delete data[key];
    }
  }
  return JSON.parse(JSON.stringify(data));
}

async function setCache(key, value) {
  var _a;
  if (!value && value !== false)
    return value;
  try {
    await ((_a = localforage.localforage === null || localforage.localforage === void 0 ? void 0 : localforage.localforage.setItem) === null || _a === void 0 ? void 0 : _a.call(localforage.localforage, key, await cleanObjectOfReferences(value)));
  }
  catch (error) {
    console.log(key, value, "Error setting data from cache with localforage", error);
  }
  return value;
}

var _a, _b, _c, _d, _e, _f;
const { state, onChange } = index.createStore({
  colors: {
    primary: "#2473bb",
    "primary-rgb": "36, 115, 187",
    "primary-contrast": "#ffffff",
    "primary-contrast-rgb": "255, 255, 255",
    "primary-shade": "#2065a5",
    "primary-tint": "#3a81c2",
    secondary: "#1e3955",
    "secondary-rgb": "30, 57, 85",
    "secondary-contrast": "#ffffff",
    "secondary-contrast-rgb": "255, 255, 255",
    "secondary-shade": "#1a324b",
    "secondary-tint": "#354d66",
    tertiary: "#54b6e7",
    "tertiary-rgb": "84, 182, 231",
    "tertiary-contrast": "#ffffff",
    "tertiary-contrast-rgb": "0, 0, 0",
    "tertiary-shade": "#4aa0cb",
    "tertiary-tint": "#65bde9",
    success: "#79c79d",
    "success-rgb": "121, 199, 157",
    "success-contrast": "#000000",
    "success-contrast-rgb": "0, 0, 0",
    "success-shade": "#6aaf8a",
    "success-tint": "#86cda7",
    warning: "#ffc723",
    "warning-rgb": "255, 199, 35",
    "warning-contrast": "#000000",
    "warning-contrast-rgb": "0, 0, 0",
    "warning-shade": "#e0af1f",
    "warning-tint": "#ffcd39",
    danger: "#db3a5e",
    "danger-rgb": "219, 58, 94",
    "danger-contrast": "#ffffff",
    "danger-contrast-rgb": "255, 255, 255",
    "danger-shade": "#c13353",
    "danger-tint": "#df4e6e",
    dark: "#08141c",
    "dark-rgb": "8, 20, 28",
    "dark-contrast": "#ffffff",
    "dark-contrast-rgb": "255, 255, 255",
    "dark-shade": "#071219",
    "dark-tint": "#212c33",
    medium: "#d8d8d8",
    "medium-rgb": "190, 190, 190",
    "medium-contrast": "#000000",
    "medium-contrast-rgb": "0, 0, 0",
    "medium-shade": "#bebebe",
    "medium-tint": "#dcdcdc",
    light: "#F6F6F6",
    "light-rgb": "248, 248, 248",
    "light-contrast": "#000000",
    "light-contrast-rgb": "0, 0, 0",
    "light-shade": "#d8d8d8",
    "light-tint": "#f7f7f7",
  },
  darkMode: (((_a = localStorage === null || localStorage === void 0 ? void 0 : localStorage.getItem) === null || _a === void 0 ? void 0 : _a.call(localStorage, "floodteam:darkMode")) &&
    JSON.parse(localStorage.getItem("floodteam:darkMode"))) ||
    !!((_c = (_b = window === null || window === void 0 ? void 0 : window.matchMedia) === null || _b === void 0 ? void 0 : _b.call(window, "(prefers-color-scheme: dark)")) === null || _c === void 0 ? void 0 : _c.matches),
  manifest: {},
  config: env(),
  version: "1.2.0",
  locations: [],
  materials: [],
  profile: {},
  session: (((_d = localStorage === null || localStorage === void 0 ? void 0 : localStorage.getItem) === null || _d === void 0 ? void 0 : _d.call(localStorage, "floodteam:session")) &&
    JSON.parse(localStorage.getItem("floodteam:session"))) ||
    null,
  services: [],
  templates: [],
  isOnline: false,
  claims: {},
  job: null,
  site: null,
  jobs: {},
  users: {},
  pins: [],
  filters: {},
  editorSizes: [50, 50],
  isPreviewing: false,
  navigationView: ((_e = localStorage === null || localStorage === void 0 ? void 0 : localStorage.getItem) === null || _e === void 0 ? void 0 : _e.call(localStorage, "floodteam:navigationView")) || "menu",
  helpers: getHandlebarsHelpers.getHandlebarsHelpers(),
  paymentMethods: [],
  roles: [
    "homeowner",
    "franchisee",
    "mitigation",
    "vision",
    "design",
    "tech",
    "finance",
    "operations",
    "success",
    "marketing",
    "agent",
    "adjuster",
    "referral",
    "vendor",
    "business",
  ],
  feedFetchData: {},
  draftNote: (((_f = localStorage === null || localStorage === void 0 ? void 0 : localStorage.getItem) === null || _f === void 0 ? void 0 : _f.call(localStorage, "floodteam:draftNote")) &&
    JSON.parse(localStorage.getItem("floodteam:draftNote"))) ||
    {},
});
onChange("darkMode", async (value) => {
  localStorage.setItem("floodteam:darkMode", JSON.stringify(value));
  if (value) {
    document.body.classList.remove("light");
    document.body.classList.add("dark");
  }
  else {
    document.body.classList.remove("dark");
    document.body.classList.add("light");
  }
});
onChange("editorSizes", async (value) => {
  await setCache("floodteam:editorSizes", value);
});
onChange("isPreviewing", async (value) => {
  await setCache("floodteam:isPreviewing", value);
});
onChange("filters", async (value) => {
  await setCache("floodteam:filters", value);
});
onChange("pins", async (value) => {
  await setCache("floodteam:pins", value);
});
onChange("jobs", async (value) => {
  if (!value)
    return;
  await setCache("floodteam:jobs", value);
});
onChange("users", async (value) => {
  if (!value)
    return;
  await setCache("floodteam:users", value);
});
onChange("claims", async (value) => {
  await setCache("floodteam:claims", value);
});
onChange("navigationView", async (value) => {
  localStorage.setItem("floodteam:navigationView", value);
});
onChange("profile", async (value) => {
  await setCache("floodteam:profile", value);
});
onChange("services", async (value) => {
  await setCache("floodteam:services", value);
});
onChange("materials", async (value) => {
  await setCache("floodteam:materials", value);
});
onChange("locations", async (value) => {
  await setCache("floodteam:locations", value);
});
onChange("session", async (value) => {
  localStorage.setItem("floodteam:session", JSON.stringify(value));
});
onChange("draftNote", async (value) => {
  localStorage.setItem("floodteam:draftNote", JSON.stringify(value));
});

exports.cleanObjectOfReferences = cleanObjectOfReferences;
exports.env = env;
exports.state = state;
