import { Component, h, Prop, State, Event } from '@stencil/core';
import { joinCommunity, isCoreTeamMember } from '../../utils/utils';
import { defaultValidator, getValidator } from '../../validators/validator.factory.js';
const rolesIds = {
  Founder: 1,
  Contributor: 2,
  Investor: 3,
};
export class RolesScreenNewUser {
  constructor() {
    this.isInvalid = false;
    this.memberRoles = [];
    this.roleSelected = { role: '', roleId: '' };
    this._commitmentValidator = defaultValidator;
  }
  componentWillLoad() {
    this._commitmentValidator = getValidator(this.validator['commitment']);
    this.assignMemberRoles();
  }
  componentWillUpdate() {
    this._commitmentValidator = getValidator(this.validator['commitment']);
  }
  updateValue(event) {
    this.skill = parseInt(event.target.value);
    if (!getValidator({ name: 'commitment', options: { min: 1 } }).validate(this.skill)) {
      this.isInvalid = true;
    }
    else {
      this.isInvalid = false;
    }
    return this.isInvalid;
  }
  async assignMemberRoles() {
    var _a, _b;
    console.log(this.community);
    const isCoreMember = await isCoreTeamMember(this.community.address, window.ethereum.selectedAddress);
    const roles = ((_b = (_a = this.community) === null || _a === void 0 ? void 0 : _a.roles) === null || _b === void 0 ? void 0 : _b.roles) || [];
    const newUserRolesBaseId = 4;
    console.log('isCoreMember: -----', isCoreMember);
    this.memberRoles = roles
      .filter(r => r.isCoreTeamMember === isCoreMember)
      .map((curr, index) => {
      const roleName = curr.roleName;
      const id = rolesIds[roleName];
      if (id) {
        curr.roleId = id;
      }
      else {
        curr.roleId = newUserRolesBaseId + index;
      }
      return curr;
    });
    console.log('Members Roles: ----', this.memberRoles);
  }
  handleRoleClick(role) {
    console.log('comm ', this.community);
    console.log('clicked', role);
    this.roleSelected = role;
    this.buttonClass = '';
  }
  async handleUserQRClick() {
    this.isLoadingEvent.emit(true);
    const tokenId = await joinCommunity(this.web3Provider, this.community.address, window.sessionStorage.getItem('username'), this.roleSelected, this.skill);
    this.isLoadingEvent.emit(false);
    window.sessionStorage.setItem('tokenId', tokenId);
    this.showNewScreen.emit('role');
  }
  render() {
    return (h("div", { class: "roles-screen-new-user" },
      h("div", { class: "user-role-header" },
        h("h2", null,
          "Your Role in ",
          h("span", { style: { textDecoration: 'underline', fontWeight: 'bold' } }, this.community.name)),
        h("h3", null, "Pick what you're the best at & be rewarded for your commitment!")),
      (this.roleSelected['role'] !== '') ?
        h("div", { class: "commitment-level-parent-div" },
          h("div", { class: "commitment-level-div" },
            h("div", { class: "commitment-level-text" },
              h("h3", null, this.roleSelected['role'])),
            h("div", { class: "xp-component" },
              h("h4", null,
                "Your ",
                h("u", null, "Commitment Level")),
              h("p", null, "Tell your Community how much time you commit to this Role!"),
              (this.isInvalid) ? h("span", { class: "validation-error" }, " Your commitment level must be atleast 1") : null,
              h("div", { class: "bar-chart-first-container" },
                h("input", { class: "bar-chart-container", type: "range", id: "myRange", min: "1", max: "10", value: this.skill, onChange: (e) => this.updateValue(e), ref: ele => this.slider = ele }),
                h("div", { class: "bar-chart-metrics" },
                  h("p", null, "1"),
                  h("p", null, "10")))))) :
        h("div", { class: "role-fields" }, this.memberRoles.map(({ roleName, roleId }) => {
          return (h("button", { class: "role-button", onClick: () => this.handleRoleClick({ role: roleName, roleId }) },
            h("h4", null, roleName)));
        })),
      h("div", { class: "step-btn-wrapper" },
        h("button", { onClick: () => this.handleUserQRClick(), class: `${this.buttonClass} step-button`, disabled: this.roleSelected['role'] === '' || this.isInvalid || this.isLoading }, "That's it - join this community!"))));
  }
  static get is() { return "roles-screen-new-user"; }
  static get encapsulation() { return "shadow"; }
  static get originalStyleUrls() { return {
    "$": ["skillwallet-auth.scss"]
  }; }
  static get styleUrls() { return {
    "$": ["skillwallet-auth.css"]
  }; }
  static get properties() { return {
    "roleSelected": {
      "type": "unknown",
      "mutable": true,
      "complexType": {
        "original": "object",
        "resolved": "object",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "defaultValue": "{ role: '', roleId: '' }"
    },
    "isLoading": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "is-loading",
      "reflect": false
    },
    "buttonClass": {
      "type": "string",
      "mutable": true,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "button-class",
      "reflect": false
    },
    "community": {
      "type": "any",
      "mutable": false,
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "community",
      "reflect": false
    },
    "isPartner": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Boolean",
        "resolved": "Boolean",
        "references": {
          "Boolean": {
            "location": "global"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "web3Provider": {
      "type": "any",
      "mutable": true,
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "web-3-provider",
      "reflect": false
    },
    "skill": {
      "type": "number",
      "mutable": true,
      "complexType": {
        "original": "number",
        "resolved": "number",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "skill",
      "reflect": false
    },
    "validator": {
      "type": "any",
      "mutable": false,
      "complexType": {
        "original": "string | any",
        "resolved": "any",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "validator",
      "reflect": false
    }
  }; }
  static get states() { return {
    "isInvalid": {},
    "memberRoles": {}
  }; }
  static get events() { return [{
      "method": "showNewScreen",
      "name": "showNewScreen",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }, {
      "method": "isLoadingEvent",
      "name": "isLoadingEvent",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "Boolean",
        "resolved": "Boolean",
        "references": {
          "Boolean": {
            "location": "global"
          }
        }
      }
    }]; }
}
