'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-36b3af04.js');
const User = require('./User-286a0714.js');
const product = require('./product-0222e027.js');
require('./data-model-c51de090.js');

const DevingenLoginPage = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
  }
  render() {
    // TODO handle redirect param
    const r = '/'; // const { r = '/' } = this.history.location.query;
    return (index.h(index.Host, { class: "container-fluid" }, index.h("div", { class: "row" }, index.h("div", { class: "col d-flex justify-content-center mt-3" }, index.h("dvn-card", null, index.h("kimlik-login-form", { api: this.apiKimlik, redirect: r }))))));
  }
};

class WorkspaceOwnership extends product.BaseModel {
  static decode(json) {
    const item = Object.assign(Object.create(WorkspaceOwnership.prototype), json);
    if (item.user)
      item.user = User.User.decode(item.user);
    if (item.workspace)
      item.workspace = product.Workspace.decode(item.workspace);
    return item;
  }
}

const DevingenRoot = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.fetchSession = async () => {
      this.fetchSessionError = undefined;
      const sessionResponse = await this.apiKimlik.getSession().catch((e) => {
        this.fetchSessionError = e;
      });
      if (!sessionResponse)
        return;
      this.user = User.User.decode(sessionResponse.user);
    };
    this.fetchWorkspaces = async () => {
      const workspaceOwnershipsResponse = await this.apiDevingen.getWorkspaceOwnerships().catch((e) => {
        this.fetchWorkspacesError = e;
      });
      if (!workspaceOwnershipsResponse)
        return;
      this.workspaceOwnerships = workspaceOwnershipsResponse.results.map(i => WorkspaceOwnership.decode(i));
      const productsResponse = await this.apiDevingen.getProducts().catch((e) => {
        this.fetchWorkspacesError = e;
      });
      if (!productsResponse)
        return;
      this.products = productsResponse.results.map(i => product.Product.decode(i));
    };
  }
  onInvalidation(_event) {
    // new workspace or product is created
    this.fetchWorkspaces();
  }
  ;
  componentWillLoad() {
    this.fetchSession();
    this.fetchWorkspaces();
  }
  render() {
    const { apiData, apiDevingen, apiKimlik, fetchSessionError, fetchWorkspacesError, products, workspaceOwnerships, user, } = this;
    if (fetchSessionError) {
      return (index.h(index.Host, null, index.h("devingen-top-bar", { user: user }), index.h("div", { class: "d-flex justify-content-center" }, index.h("devingen-error-card", { error: fetchSessionError, message: "Failed to get session info.", onRetry: this.fetchSession }))));
    }
    if (fetchWorkspacesError) {
      return (index.h(index.Host, null, index.h("div", { class: "d-flex justify-content-center" }, index.h("devingen-error-card", { error: fetchWorkspacesError, message: "Failed to get workspace info.", onRetry: this.fetchWorkspaces }))));
    }
    return (index.h(index.Host, null, index.h("devingen-top-bar", { user: user }), index.h("stencil-router", null, index.h("stencil-route-switch", { scrollTopOffset: 0 }, index.h("stencil-route", { url: "/", component: "devingen-home-page", componentProps: { apiData, apiDevingen, apiKimlik, products, workspaceOwnerships }, exact: true }), index.h("stencil-route", { url: "/products/:id", component: "devingen-product-page", componentProps: { apiData, apiDevingen, apiKimlik, products, workspaceOwnerships } }), index.h("stencil-route", { component: "devingen-page-not-found" })))));
  }
};

exports.devingen_login_page = DevingenLoginPage;
exports.devingen_router = DevingenRoot;
