import { r as registerInstance, h, H as Host } from './index-b7735e0d.js';
import { U as User } from './User-1ef5a482.js';
import './data-model-f7ccf826.js';

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

class BaseModel {
}

class Workspace extends BaseModel {
  static decode(json) {
    return Object.assign(Object.create(Workspace.prototype), json);
  }
}

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

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

let DevingenRoot = class {
  constructor(hostRef) {
    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.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.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 (h(Host, null, h("devingen-top-bar", { user: user }), h("div", { class: "d-flex justify-content-center" }, h("devingen-error-card", { error: fetchSessionError, message: "Failed to get session info.", onRetry: this.fetchSession }))));
    }
    if (fetchWorkspacesError) {
      return (h(Host, null, h("div", { class: "d-flex justify-content-center" }, h("devingen-error-card", { error: fetchWorkspacesError, message: "Failed to get workspace info.", onRetry: this.fetchWorkspaces }))));
    }
    return (h(Host, null, h("devingen-top-bar", { user: user }), h("stencil-router", null, h("stencil-route-switch", { scrollTopOffset: 0 }, h("stencil-route", { url: "/", component: "devingen-home-page", componentProps: { apiData, apiDevingen, apiKimlik, products, workspaceOwnerships }, exact: true }), h("stencil-route", { url: "/products/:id", component: "devingen-product-page", componentProps: { apiData, apiDevingen, apiKimlik, products, workspaceOwnerships } }), h("stencil-route", { component: "devingen-page-not-found" })))));
  }
};

export { DevingenLoginPage as devingen_login_page, DevingenRoot as devingen_router };
