import { injectNotifier } from '@dvn/alaca/global/notifier';
import { TextField } from '@dvn/core/doldurma/src/fields/text';
import { ModifierTrimmer } from '@dvn/core/doldurma/src/interceptors/modifiers/ModifierTrimmer';
import { ValidatorLength } from '@dvn/core/doldurma/src/interceptors/validators/ValidatorLength';
import { Component, h, Host, Listen, Prop, State } from '@stencil/core';
import * as formViewProductCreate from '../../configs/view-configs/form-view-products-create';
import * as formViewWorkspaceCreate from '../../configs/view-configs/form-view-workspace-create';
export class DevingenHomePage {
  constructor() {
    this.showCreateWorkspaceForm = false;
    this.showCreateProductForm = false;
    this.showEditWorkspaceForm = false;
    this.showDeleteConfirmation = false;
    this.renderEditWorkspaceForm = () => {
      var _a;
      if (!this.showEditWorkspaceForm) {
        return null;
      }
      return (h("dvn-dialog", { shown: true, onDismiss: () => {
          this.showEditWorkspaceForm = false;
          this.selectedWorkspace = undefined;
        } },
        h("doldurma-form", { fields: [
            new TextField('name', 'Name').addInterceptor('onSubmit', new ValidatorLength(2, 32))
              .addInterceptor('onSubmit', new ModifierTrimmer())
              .setValue((_a = this.selectedWorkspace) === null || _a === void 0 ? void 0 : _a.name)
              .require(),
          ], onSubmit: this.onSubmitEditWorkspaceForm, submitButtonLabel: "Update", extraButtons: [{
              label: 'Delete',
              tint: 'danger',
              view: 'flat',
              onClick: () => {
                this.showDeleteConfirmation = true;
              },
            }], formErrors: this.updateError ? ['Update failed.'] : undefined })));
    };
    this.renderDeleteWorkspaceConfirmation = () => {
      var _a;
      if (!this.showDeleteConfirmation) {
        return null;
      }
      return (h("dvn-dialog", { shown: true, onDismiss: () => {
          this.showDeleteConfirmation = false;
          this.deleteError = undefined;
        }, title: "Deletion" },
        h("p", null, "Are you sure to delete this product? This action cannot be undone."),
        this.deleteError &&
          h("p", { class: "text-danger" }, ((_a = this.deleteError.data) === null || _a === void 0 ? void 0 : _a.error) === 'workspace-has-products'
            ? 'You need to delete all products belong to this workspace before deleting it.'
            : 'Delete failed.'),
        h("div", { slot: "actions" },
          h("dvn-button", { view: "flat", onClick: () => {
              this.showDeleteConfirmation = false;
              this.deleteError = undefined;
            } }, "Cancel"),
          h("dvn-button", { tint: "danger", onClick: this.onConfirmDeleteWorkspace }, "Yes, Delete"))));
    };
    this.onSubmitEditWorkspaceForm = (event) => {
      const { values } = event.detail;
      this.apiDevingen.updateWorkspace(this.selectedWorkspace._id, Object.assign(Object.assign({}, this.selectedWorkspace), { name: values.name }))
        .then(() => {
        location.reload();
      })
        .catch(error => this.updateError = error);
    };
    this.onConfirmDeleteWorkspace = () => {
      this.apiDevingen.deleteWorkspace(this.selectedWorkspace._id)
        .then(() => location.pathname = '/')
        .catch(error => this.deleteError = error);
    };
  }
  onInvalidation(_event) {
    // new workspace or product is created
    this.showCreateWorkspaceForm = false;
    this.showCreateProductForm = false;
  }
  ;
  render() {
    const { showCreateProductForm, showCreateWorkspaceForm, 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("div", { class: "d-flex align-items-center" },
              h("h2", { class: "mt-3" }, workspace.name),
              h("dvn-icon-button", { iconName: "uil uil-cog", scale: "small", view: "flat", onClick: () => {
                  this.selectedWorkspace = workspace;
                  this.showEditWorkspaceForm = true;
                } })),
            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.showCreateProductForm = 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.showCreateWorkspaceForm = true, view: "flat" },
              h("dvn-icon", { iconName: `uil uil-plus` }),
              "Add new workspace")))),
      showCreateWorkspaceForm &&
        h("dvn-dialog", { raw: true, shown: true, onDismiss: () => this.showCreateWorkspaceForm = false },
          h("dvn-app-view-form-view", { view: formViewWorkspaceCreate.config(this.apiDevingen) })),
      showCreateProductForm &&
        h("dvn-dialog", { raw: true, shown: true, onDismiss: () => this.showCreateProductForm = false },
          h("dvn-app-view-form-view", { view: formViewProductCreate.config(this.apiDevingen), params: {
              workspaceId: selectedWorkspaceToCreateProduct,
              type: 'damga',
            } })),
      this.renderEditWorkspaceForm(),
      this.renderDeleteWorkspaceConfirmation()));
  }
  static get is() { return "devingen-home-page"; }
  static get properties() { return {
    "apiDevingen": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "DevingenApi",
        "resolved": "DevingenApi",
        "references": {
          "DevingenApi": {
            "location": "import",
            "path": "../../api/devingen-api"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "apiKimlik": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "KimlikApi",
        "resolved": "KimlikApi",
        "references": {
          "KimlikApi": {
            "location": "import",
            "path": "@dvn/kimlik/api/kimlikApi"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "notifier": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Notifier",
        "resolved": "Notifier",
        "references": {
          "Notifier": {
            "location": "import",
            "path": "@dvn/alaca/global/notifier/models"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "products": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Product[]",
        "resolved": "Product[]",
        "references": {
          "Product": {
            "location": "import",
            "path": "../../models/product"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "workspaceOwnerships": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "WorkspaceOwnership[]",
        "resolved": "WorkspaceOwnership[]",
        "references": {
          "WorkspaceOwnership": {
            "location": "import",
            "path": "../../models/workspace-ownership"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    }
  }; }
  static get states() { return {
    "showCreateWorkspaceForm": {},
    "showCreateProductForm": {},
    "selectedWorkspaceToCreateProduct": {},
    "updateError": {},
    "deleteError": {},
    "showEditWorkspaceForm": {},
    "showDeleteConfirmation": {},
    "selectedWorkspace": {}
  }; }
  static get listeners() { return [{
      "name": "invalidation",
      "method": "onInvalidation",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
injectNotifier(DevingenHomePage);
