import { r as registerInstance, B as Build, h } from './index-33e43734.js';
import { t as toastController } from './overlays-97615cf7.js';
import { g as gql, D as DatabaseService, F as FireEnjin } from './index-14289339.js';
import { i as initializeApp } from './index.esm2017-8e70fa12.js';
import { e as env, s as state } from './store-e398cba7.js';
import { A as AuthService } from './auth-6f1021a8.js';
import './ionic-global-bd76bde2.js';
import './helpers-a9270f9e.js';
import './index-9d30674a.js';
import './process-es6-6fcfb3fc.js';
import './global-e1c7e609.js';
import './tslib.es6-78d2ab52.js';
import './localforage-9915b7c8.js';
import './_commonjsHelpers-cca61ea8.js';
import './index-91eb4513.js';

const AddProjectDocument = gql `
  mutation addProject($data: ProjectInput!) {
    addProject(data: $data) {
      id
    }
  }
`;
const DeleteProjectDocument = gql `
  mutation deleteProject($id: String!) {
    deleteProject(id: $id) {
      id
    }
  }
`;
const EditProjectDocument = gql `
  mutation editProject($id: String!, $data: ProjectInput!) {
    editProject(id: $id, data: $data) {
      id
    }
  }
`;
const FindProjectDocument = gql `
  query findProject($id: String!) {
    project(id: $id) {
      id
    }
  }
`;
const ListProjectsDocument = gql `
  query listProjects($data: ListQueryInput!) {
    projects(data: $data) {
      id
    }
  }
`;
const AddTemplateDocument = gql `
  mutation addTemplate($data: TemplateInput!) {
    addTemplate(data: $data) {
      id
    }
  }
`;
const DeleteTemplateDocument = gql `
  mutation deleteTemplate($id: String!) {
    deleteTemplate(id: $id) {
      id
    }
  }
`;
const EditTemplateDocument = gql `
  mutation editTemplate($id: String!, $data: TemplateInput!) {
    editTemplate(id: $id, data: $data) {
      id
    }
  }
`;
const FindTemplateDocument = gql `
  query findTemplate($id: String!) {
    template(id: $id) {
      id
      editor {
        time
        version
        blocks {
          data
          type
        }
      }
      tags
      description
      routes
      name
      subject
      type
      html
      head
      text
      amp
      header {
        id
        subject
        description
        html
      }
      footer {
        id
        subject
        description
        html
      }
      frame {
        id
        subject
        description
        html
        head
        header {
          id
          subject
          description
          html
        }
        footer {
          id
          subject
          description
          html
        }
      }
      attachments {
        cid
        filename
        content
        path
        encoding
        raw
        href
        httpHeaders
        contentDisposition
        contentType
        headers
      }
      partial
      sampleData
      createdAt
    }
  }
`;
const ListTemplatesDocument = gql `
  query listTemplates($data: TemplateListInput!, $withHtml: Boolean!) {
    templates(data: $data) {
      id
      name
      routes
      description
      subject
      type
      text
      html @include(if: $withHtml)
      head @include(if: $withHtml)
      header @include(if: $withHtml) {
        id
        subject
        description
        html
      }
      footer @include(if: $withHtml) {
        id
        subject
        description
        html
      }
      frame @include(if: $withHtml) {
        id
        subject
        description
        html
      }
      tags
      partial
      createdAt
    }
  }
`;
const AddTokenDocument = gql `
  mutation addToken($data: TokenInput!) {
    addToken(data: $data) {
      id
    }
  }
`;
const DeleteTokenDocument = gql `
  mutation deleteToken($id: String!) {
    deleteToken(id: $id) {
      id
    }
  }
`;
const EditTokenDocument = gql `
  mutation editToken($id: String!, $data: TokenInput!) {
    editToken(id: $id, data: $data) {
      id
    }
  }
`;
const FindTokenDocument = gql `
  query findToken($id: String!) {
    token(id: $id) {
      id
    }
  }
`;
const ListTokensDocument = gql `
  query listTokens($data: ListQueryInput!) {
    tokens(data: $data) {
      id
    }
  }
`;
const AddUserDocument = gql `
  mutation addUser($data: UserInput!) {
    addUser(data: $data) {
      id
    }
  }
`;
const DeleteUserDocument = gql `
  mutation deleteUser($id: String!) {
    deleteUser(id: $id) {
      id
    }
  }
`;
const EditUserDocument = gql `
  mutation editUser($id: String!, $data: UserInput!) {
    editUser(id: $id, data: $data) {
      id
    }
  }
`;
const FindUserDocument = gql `
  query findUser($id: String!) {
    user(id: $id) {
      id
    }
  }
`;
const ListUsersDocument = gql `
  query listUsers($data: ListQueryInput!) {
    users(data: $data) {
      id
    }
  }
`;
const GeneratePdfDocument = gql `
  mutation generatePdf($data: GeneratePdfInput!) {
    generatePdf(data: $data) {
      id
      url
    }
  }
`;
const SendTemplateEmailDocument = gql `
  mutation sendTemplateEmail($data: SendTemplateEmailInput!) {
    sendTemplateEmail(data: $data) {
      id
    }
  }
`;
const defaultWrapper = (action, _operationName, _operationType) => action();
function getSdk(client, withWrapper = defaultWrapper) {
  return {
    addProject(variables, requestHeaders) {
      return withWrapper((wrappedRequestHeaders) => client.request(AddProjectDocument, variables, {
        ...requestHeaders,
        ...wrappedRequestHeaders,
      }), "addProject", "mutation");
    },
    deleteProject(variables, requestHeaders) {
      return withWrapper((wrappedRequestHeaders) => client.request(DeleteProjectDocument, variables, { ...requestHeaders, ...wrappedRequestHeaders }), "deleteProject", "mutation");
    },
    editProject(variables, requestHeaders) {
      return withWrapper((wrappedRequestHeaders) => client.request(EditProjectDocument, variables, {
        ...requestHeaders,
        ...wrappedRequestHeaders,
      }), "editProject", "mutation");
    },
    findProject(variables, requestHeaders) {
      return withWrapper((wrappedRequestHeaders) => client.request(FindProjectDocument, variables, {
        ...requestHeaders,
        ...wrappedRequestHeaders,
      }), "findProject", "query");
    },
    listProjects(variables, requestHeaders) {
      return withWrapper((wrappedRequestHeaders) => client.request(ListProjectsDocument, variables, {
        ...requestHeaders,
        ...wrappedRequestHeaders,
      }), "listProjects", "query");
    },
    addTemplate(variables, requestHeaders) {
      return withWrapper((wrappedRequestHeaders) => client.request(AddTemplateDocument, variables, {
        ...requestHeaders,
        ...wrappedRequestHeaders,
      }), "addTemplate", "mutation");
    },
    deleteTemplate(variables, requestHeaders) {
      return withWrapper((wrappedRequestHeaders) => client.request(DeleteTemplateDocument, variables, { ...requestHeaders, ...wrappedRequestHeaders }), "deleteTemplate", "mutation");
    },
    editTemplate(variables, requestHeaders) {
      return withWrapper((wrappedRequestHeaders) => client.request(EditTemplateDocument, variables, { ...requestHeaders, ...wrappedRequestHeaders }), "editTemplate", "mutation");
    },
    findTemplate(variables, requestHeaders) {
      return withWrapper((wrappedRequestHeaders) => client.request(FindTemplateDocument, variables, {
        ...requestHeaders,
        ...wrappedRequestHeaders,
      }), "findTemplate", "query");
    },
    listTemplates(variables, requestHeaders) {
      return withWrapper((wrappedRequestHeaders) => client.request(ListTemplatesDocument, variables, {
        ...requestHeaders,
        ...wrappedRequestHeaders,
      }), "listTemplates", "query");
    },
    addToken(variables, requestHeaders) {
      return withWrapper((wrappedRequestHeaders) => client.request(AddTokenDocument, variables, {
        ...requestHeaders,
        ...wrappedRequestHeaders,
      }), "addToken", "mutation");
    },
    deleteToken(variables, requestHeaders) {
      return withWrapper((wrappedRequestHeaders) => client.request(DeleteTokenDocument, variables, {
        ...requestHeaders,
        ...wrappedRequestHeaders,
      }), "deleteToken", "mutation");
    },
    editToken(variables, requestHeaders) {
      return withWrapper((wrappedRequestHeaders) => client.request(EditTokenDocument, variables, {
        ...requestHeaders,
        ...wrappedRequestHeaders,
      }), "editToken", "mutation");
    },
    findToken(variables, requestHeaders) {
      return withWrapper((wrappedRequestHeaders) => client.request(FindTokenDocument, variables, {
        ...requestHeaders,
        ...wrappedRequestHeaders,
      }), "findToken", "query");
    },
    listTokens(variables, requestHeaders) {
      return withWrapper((wrappedRequestHeaders) => client.request(ListTokensDocument, variables, {
        ...requestHeaders,
        ...wrappedRequestHeaders,
      }), "listTokens", "query");
    },
    addUser(variables, requestHeaders) {
      return withWrapper((wrappedRequestHeaders) => client.request(AddUserDocument, variables, {
        ...requestHeaders,
        ...wrappedRequestHeaders,
      }), "addUser", "mutation");
    },
    deleteUser(variables, requestHeaders) {
      return withWrapper((wrappedRequestHeaders) => client.request(DeleteUserDocument, variables, {
        ...requestHeaders,
        ...wrappedRequestHeaders,
      }), "deleteUser", "mutation");
    },
    editUser(variables, requestHeaders) {
      return withWrapper((wrappedRequestHeaders) => client.request(EditUserDocument, variables, {
        ...requestHeaders,
        ...wrappedRequestHeaders,
      }), "editUser", "mutation");
    },
    findUser(variables, requestHeaders) {
      return withWrapper((wrappedRequestHeaders) => client.request(FindUserDocument, variables, {
        ...requestHeaders,
        ...wrappedRequestHeaders,
      }), "findUser", "query");
    },
    listUsers(variables, requestHeaders) {
      return withWrapper((wrappedRequestHeaders) => client.request(ListUsersDocument, variables, {
        ...requestHeaders,
        ...wrappedRequestHeaders,
      }), "listUsers", "query");
    },
    generatePdf(variables, requestHeaders) {
      return withWrapper((wrappedRequestHeaders) => client.request(GeneratePdfDocument, variables, {
        ...requestHeaders,
        ...wrappedRequestHeaders,
      }), "generatePdf", "mutation");
    },
    sendTemplateEmail(variables, requestHeaders) {
      return withWrapper((wrappedRequestHeaders) => client.request(SendTemplateEmailDocument, variables, { ...requestHeaders, ...wrappedRequestHeaders }), "sendTemplateEmail", "mutation");
    },
  };
}

let AppRouter = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    var _a, _b, _c, _d;
    this.app = initializeApp(env("firebase", {})) ;
    this.auth = new AuthService({
        app: this.app,
        config: {
          tokenLocalStorageKey: "mx:token",
          authLocalStorageKey: "mx:session",
          emulate: !!((_a = state === null || state === void 0 ? void 0 : state.config) === null || _a === void 0 ? void 0 : _a.emulate),
        },
      })
      ;
    this.db = new DatabaseService({
        app: this.app,
        emulate: !!((_b = state === null || state === void 0 ? void 0 : state.config) === null || _b === void 0 ? void 0 : _b.emulate),
      })
      ;
    this.fireenjin = new FireEnjin({
        getSdk,
        debug: Build === null || Build === void 0 ? void 0 : Build.isDev,
        connections: [
          {
            name: "default",
            type: "graphql",
            url: ((_d = (_c = state === null || state === void 0 ? void 0 : state.config) === null || _c === void 0 ? void 0 : _c.graphql) === null || _d === void 0 ? void 0 : _d.url) || "https://fireenjin.com/graphql",
          },
        ],
      })
      ;
    this.componentProps = {
      app: this.app,
      auth: this.auth,
      db: this.db,
      fireenjin: this.fireenjin,
    };
  }
  async onUpdate() {
    const registration = await navigator.serviceWorker.getRegistration();
    if (!registration || !registration.waiting) {
      // If there is no registration, this is the first service
      // worker to be installed. registration.waiting is the one
      // waiting to be activiated.
      return;
    }
    this.onToast({
      detail: {
        toast: {
          message: "New version available",
          buttons: [
            {
              side: "end",
              text: "Reload",
              handler: () => {
                registration.waiting.postMessage("skipWaiting");
                window.location.reload();
              },
            },
          ],
        },
      },
    });
  }
  async onSuccess(event) {
    var _a;
    if (["addTemplate", "editTemplate", "deleteTemplate"].includes((_a = event === null || event === void 0 ? void 0 : event.detail) === null || _a === void 0 ? void 0 : _a.endpoint)) {
      this.getTemplates();
    }
  }
  async onToast(event) {
    var _a;
    if (!((_a = event === null || event === void 0 ? void 0 : event.detail) === null || _a === void 0 ? void 0 : _a.toast))
      return;
    const toast = await toastController.create(event.detail.toast);
    await toast.present();
  }
  async onRouteChange(event) {
    var _a, _b, _c;
    const routerEl = (event === null || event === void 0 ? void 0 : event.target) || document.querySelector("ion-router");
    let stopLoader = true;
    try {
      if (((_a = state === null || state === void 0 ? void 0 : state.session) === null || _a === void 0 ? void 0 : _a.uid) && ["/"].indexOf((_b = event === null || event === void 0 ? void 0 : event.detail) === null || _b === void 0 ? void 0 : _b.to) >= 0) {
        stopLoader = false;
        routerEl.push("/templates");
      }
      if (!((_c = state === null || state === void 0 ? void 0 : state.session) === null || _c === void 0 ? void 0 : _c.uid) &&
        ["/templates"].indexOf(event.detail.to) >= 0) {
        stopLoader = false;
        routerEl.push("/");
      }
    }
    catch (err) {
      console.log(err.message);
    }
    if (stopLoader) {
      document.body.classList.add("is-loaded");
    }
  }
  onKeydown(event) {
    if (event.key === "s" &&
      (navigator.platform.match("Mac") ? event.metaKey : event.ctrlKey)) {
      event.preventDefault();
    }
  }
  async getTemplates() {
    const templates = await this.db.list("templates", []);
    state.templates = templates || [];
    state.partials = state.templates.filter((template) => !!(template === null || template === void 0 ? void 0 : template.partial));
    return templates;
  }
  componentWillLoad() {
    if (Build === null || Build === void 0 ? void 0 : Build.isBrowser) {
      this.auth.onAuthChanged(async (session) => {
        var _a;
        if (session && session.uid) {
          state.session = session;
          if (["/", "/login"].includes((_a = window === null || window === void 0 ? void 0 : window.location) === null || _a === void 0 ? void 0 : _a.pathname)) {
            setTimeout(() => {
              const routerEl = document.querySelector("ion-router");
              if (routerEl === null || routerEl === void 0 ? void 0 : routerEl.push)
                routerEl.push("/templates");
            }, 200);
          }
          this.getTemplates();
          if (typeof (session === null || session === void 0 ? void 0 : session.uid) !== "string")
            return;
          this.db.watchDocument("users", session.uid, async (snapshot) => {
            var _a;
            if (!((_a = snapshot === null || snapshot === void 0 ? void 0 : snapshot.data) === null || _a === void 0 ? void 0 : _a.trade)) {
              this.db.add("users", {
                id: session === null || session === void 0 ? void 0 : session.uid,
                trade: "dev",
                email: (session === null || session === void 0 ? void 0 : session.email) || null,
              }, session === null || session === void 0 ? void 0 : session.uid);
            }
            else {
              state.profile = snapshot.data;
            }
          });
        }
      });
    }
  }
  render() {
    return [
      h("ion-router", { useHash: false }, h("ion-route", { url: "/", component: "fireenjin-mx-page-login", componentProps: this.componentProps }), h("ion-route", { url: "/login", component: "fireenjin-mx-page-login", componentProps: this.componentProps }), h("ion-route", { url: "/templates", component: "fireenjin-mx-page-home", componentProps: this.componentProps }), h("ion-route", { component: "fireenjin-mx-page-template", componentProps: this.componentProps, url: "/templates/:templateId" }), h("ion-route", { url: "/test", component: "fireenjin-mx-page-test", componentProps: this.componentProps })),
    ];
  }
};

export { AppRouter as fireenjin_mx_router };

//# sourceMappingURL=fireenjin-mx-router.entry.js.map