import { r as registerInstance, h, H as Host } from './index-55faf181.js';
import { i as injectNotifier } from './index-7884b902.js';
import { D as DataTransferField } from './data-transfer-field-918730a3.js';
import { S as SingleChoiceField, a as SingleChoiceFieldOption } from './index-083a6906.js';
import { T as TextField } from './index-403e215e.js';
import { F as FormViewConfig, M as ModifierTrimmer } from './form-view-config-00abddd1.js';
import { V as ValidatorLength } from './ValidatorLength-f56a7c14.js';
import { A as AppViewType } from './view-config-39fd369a.js';
import { I as InvalidationAction } from './actions-b9a0f808.js';
import './index-6a1cf9c1.js';
import { D as DateField, a as DateDataType } from './date-ce777316.js';
import { D as DataModel } from './data-model-f5160369.js';
import './state-tunnel-04c0b67a.js';
import './BaseField-461b6d0c.js';
import './InterceptorHandler-6474bcf4.js';
import './table-config-5c917348.js';
import './FieldModelGenerator-1ee7fda2.js';
import './index-fa1db470.js';

class FormHandlerCreateProduct {
  constructor(apiDevingen) {
    this.api = apiDevingen;
  }
  onSubmit(event) {
    const { values: { name, workspaceId, type } } = event.detail;
    return new Promise((resolve, reject) => {
      this.api.createProduct(name, workspaceId, type).then(() => {
        resolve({
          events: [
            new InvalidationAction('', ''),
          ],
          navigation: {
            type: 'navigation',
            direction: 'back'
          }
        });
      }).catch(() => {
        reject(['Unexpected error']);
      });
    });
  }
}

const dataConfigs = {
  'devingen-workspaces': DataModel.decode({
    _id: 'devingen-workspaces',
    base: 'devingen-dev',
    fields: [
      new DateField('_created', 'Created')
        .setDataType(DateDataType.Unix)
        .setDateFormat('DD MMM YYYY HH:mm'),
      new TextField('name', 'Name'),
    ],
    name: 'devingen-workspaces',
  }),
};

const ID$1 = 'new-product-form';
const config$1 = (apiDevingen) => FormViewConfig.decode({
  _id: ID$1,
  base: 'devingen-dev',
  title: 'New domain',
  table: dataConfigs['devingen-workspaces'],
  type: AppViewType.FormView,
  handler: new FormHandlerCreateProduct(apiDevingen),
  ui: {
    fields: [
      // this field is passed by the parameters. this is a data holder field to pass the data to form handler
      {
        field: new DataTransferField('workspaceId'),
      },
      {
        field: new TextField('name', 'Name').addInterceptor('onSubmit', new ValidatorLength(2, 32))
          .addInterceptor('onSubmit', new ModifierTrimmer())
          .require(),
      },
      {
        field: new SingleChoiceField('type', 'Product Type', [
          new SingleChoiceFieldOption('Damga', 'damga', undefined, 'Proxy service that generates SSL certificate for your domains.'),
          // new SingleChoiceFieldOption('Sepet', 'sepet', 'warning', 'CDN service for serving files.'), // yes, coming soon :)
        ], '', 'radioCard').require(),
      },
    ],
  },
});

class FormHandlerCreateWorkspace {
  constructor(apiDevingen) {
    this.api = apiDevingen;
  }
  onSubmit(event) {
    const { values: { name } } = event.detail;
    return new Promise((resolve, reject) => {
      this.api.createWorkspace(name).then(() => {
        resolve({
          events: [
            new InvalidationAction('', ''),
          ],
          navigation: {
            type: 'navigation',
            direction: 'back'
          }
        });
      }).catch(() => {
        reject(['Unexpected error']);
      });
    });
  }
}

const ID = 'new-workspace-form';
const config = (apiDevingen) => FormViewConfig.decode({
  _id: ID,
  base: 'devingen-dev',
  title: 'New domain',
  table: dataConfigs['devingen-workspaces'],
  type: AppViewType.FormView,
  handler: new FormHandlerCreateWorkspace(apiDevingen),
  ui: {
    fields: [
      {
        field: new TextField('name', 'Name').addInterceptor('onSubmit', new ValidatorLength(2, 32))
          .addInterceptor('onSubmit', new ModifierTrimmer())
          .require(),
      },
    ],
  },
});

const DevingenHomePage = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.isCreateWorkspaceFormShown = false;
    this.isCreateProductFormShown = false;
  }
  onInvalidation(_event) {
    // new workspace or product is created
    this.isCreateWorkspaceFormShown = false;
    this.isCreateProductFormShown = false;
  }
  ;
  componentWillLoad() {
  }
  render() {
    const { isCreateProductFormShown, isCreateWorkspaceFormShown, products, selectedWorkspaceToCreateProduct, workspaceOwnerships } = this;
    if (!workspaceOwnerships || !products) {
      return (h("div", { class: "d-flex justify-content-center pt-3" }, h("dvn-progress", { indefinite: true })));
    }
    return (h(Host, null, h("div", { class: "container" }, h("div", { class: "row" }, workspaceOwnerships.map(workspaceOwnership => {
      const { workspace } = workspaceOwnership;
      return (h("div", { class: "col-sm-12" }, h("h2", { class: "mt-3" }, workspace.name), h("div", { class: "row" }, products.filter(product => product.workspace._id === workspace._id).map(product => (h("div", { class: "col-sm-12 col-md-4 mb-3" }, h("stencil-route-link", { url: `/products/${product._id}` }, h("dvn-card", { bodyLeaned: true, elevation: 0, leaned: true, selectable: true, tint: "primary" }, h("devingen-product-card", { product: product })))))), h("div", { class: "col-sm-12 col-md-4" }, h("dvn-card", { bodyLeaned: true, elevation: 0, leaned: true, selectable: true, tint: "primary" }, h("devingen-product-card", { onClick: () => {
          this.isCreateProductFormShown = true;
          this.selectedWorkspaceToCreateProduct = workspace._id;
        } }))))));
    })), h("div", { class: "row" }, h("div", { class: "col-sm-12 col-md-4 mt-3 pt-3" }, h("dvn-button", { leaned: true, onClick: () => this.isCreateWorkspaceFormShown = true, view: "flat" }, h("dvn-icon", { iconName: `uil uil-plus` }), "Add new workspace")))), isCreateWorkspaceFormShown &&
      h("dvn-dialog", { raw: true, shown: true, onDismiss: () => this.isCreateWorkspaceFormShown = false }, h("dvn-app-view-form-view", { view: config(this.apiDevingen) })), isCreateProductFormShown &&
      h("dvn-dialog", { raw: true, shown: true, onDismiss: () => this.isCreateProductFormShown = false }, h("dvn-app-view-form-view", { view: config$1(this.apiDevingen), params: {
          workspaceId: selectedWorkspaceToCreateProduct,
          type: 'damga',
        } }))));
  }
};
injectNotifier(DevingenHomePage);

export { DevingenHomePage as devingen_home_page };
