'use strict';

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

const index$2 = require('./index-36b3af04.js');
const index$3 = require('./index-4d2f1ab7.js');
const dataTransferField = require('./data-transfer-field-0ce05c1a.js');
const index$1 = require('./index-fe19a98b.js');
const index = require('./index-a8f1783c.js');
const formViewConfig = require('./form-view-config-51279874.js');
const ValidatorLength = require('./ValidatorLength-06e5c6f8.js');
const viewConfig = require('./view-config-ec5f62df.js');
const actions = require('./actions-d7eabd95.js');
require('./index-74a00556.js');
const date = require('./date-25b104f6.js');
const dataModel = require('./data-model-c51de090.js');
require('./state-tunnel-786a62ce.js');
require('./BaseField-d019ade6.js');
require('./InterceptorHandler-09a1e84b.js');
require('./table-config-444a77e4.js');
require('./FieldModelGenerator-205dd99c.js');
require('./index-ae305bae.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 actions.InvalidationAction('', ''),
          ],
          navigation: {
            type: 'navigation',
            direction: 'back'
          }
        });
      }).catch(() => {
        reject(['Unexpected error']);
      });
    });
  }
}

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

const ID$1 = 'new-product-form';
const config$1 = (apiDevingen) => formViewConfig.FormViewConfig.decode({
  _id: ID$1,
  base: 'devingen-dev',
  title: 'New domain',
  table: dataConfigs['devingen-workspaces'],
  type: viewConfig.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.DataTransferField('workspaceId'),
      },
      {
        field: new index.TextField('name', 'Name').addInterceptor('onSubmit', new ValidatorLength.ValidatorLength(2, 32))
          .addInterceptor('onSubmit', new formViewConfig.ModifierTrimmer())
          .require(),
      },
      {
        field: new index$1.SingleChoiceField('type', 'Product Type', [
          new index$1.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 actions.InvalidationAction('', ''),
          ],
          navigation: {
            type: 'navigation',
            direction: 'back'
          }
        });
      }).catch(() => {
        reject(['Unexpected error']);
      });
    });
  }
}

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

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

exports.devingen_home_page = DevingenHomePage;
