import { Component, Element, Event, h, Host, Method, Prop, State } from '@stencil/core';
export class CardSteps {
  constructor() {
    this.stepOptions = [];
    this.label = '';
    /**
     * If `true` a the style is ready for a dark background.
     */
    this.inverted = false;
    /**
     * If `true` the steps navigation is hidden.
     */
    this.hidden = false;
    /**
     * If `true` the steps navigation has back button.
     */
    this.hasBack = false;
    /**
     * If `true` the navigation is handled by the component
     */
    this.navigation = false;
    /**
     * Label for back button
     */
    this.backLabel = '';
    /**
     * Hides the navigation circles and adds the step label instead
     */
    this.showLabel = false;
  }
  /**
   * Go to tab with the given value
   */
  async select(step) {
    this.steps.forEach(t => t.setActive(t.value === step.value));
    this.sync();
    this.balChange.emit(step);
  }
  async sync() {
    await Promise.all(this.steps.map(value => value.getOptions())).then(stepOptions => {
      this.stepOptions = stepOptions;
    });
    this.label = this.stepOptions.find(o => o.active).label;
  }
  componentWillLoad() {
    this.sync();
  }
  get steps() {
    return Array.from(this.element.querySelectorAll('bal-card-step'));
  }
  async onClickStepCircle(step) {
    if (this.navigation && !step.disabled) {
      await this.select(step);
    }
    this.balStepClick.emit(step);
  }
  async onBackButtonClick() {
    if (this.navigation) {
      let previousStepIndex = this.getPreviousStepIndex();
      let previousStep = this.stepOptions[previousStepIndex];
      if (previousStepIndex >= 0 && previousStep && !previousStep.disabled) {
        await this.select(previousStep);
      }
    }
    this.balBackClick.emit();
  }
  getPreviousStepIndex() {
    let activeStepIndex = this.stepOptions.findIndex(el => el.active === true);
    return activeStepIndex - 1;
  }
  render() {
    return (h(Host, null,
      h("div", { class: ['card-steps-wrapper', this.inverted ? 'is-inverted' : ''].join(' ') },
        h("div", { class: ['left-side', !this.hasBack ? 'is-hidden' : ''].join(' ') },
          h("a", { role: "button", onClick: () => this.onBackButtonClick() },
            h("bal-icon", { class: "nav-go-left", name: "nav-go-large", size: "medium", color: this.inverted ? 'white' : 'blue' }),
            h("span", { class: "nav-go-left-label" }, this.backLabel))),
        h("h3", { style: { display: this.showLabel ? 'block' : 'none' }, class: ['tab-title title is-size-3', this.inverted ? 'is-inverted' : ''].join(' ') }, this.label),
        h("div", { style: { display: this.showLabel ? 'none' : 'block' }, class: [
            'tabs',
            !this.navigation ? 'is-disabled' : '',
            this.inverted ? 'is-inverted' : '',
            this.hidden ? 'is-hidden' : '',
          ].join(' ') },
          h("ul", null, this.stepOptions
            .filter(step => !step.hidden && !this.hidden && !this.showLabel)
            .map((step, index) => (h("li", { class: [
              step.active ? 'is-active' : '',
              step.disabled ? 'is-disabled' : '',
              step.done ? 'is-done' : '',
            ].join(' ') },
            h("a", { onClick: () => this.onClickStepCircle(step), title: step.label },
              h("span", { class: "step-index" },
                h("span", null, index + 1)))))))),
        h("div", { class: ['right-side', !this.hasBack ? 'is-hidden' : ''].join(' ') })),
      h("slot", null)));
  }
  static get is() { return "bal-card-steps"; }
  static get encapsulation() { return "scoped"; }
  static get originalStyleUrls() { return {
    "$": ["bal-card-steps.scss"]
  }; }
  static get styleUrls() { return {
    "$": ["bal-card-steps.css"]
  }; }
  static get properties() { return {
    "inverted": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "If `true` a the style is ready for a dark background."
      },
      "attribute": "inverted",
      "reflect": false,
      "defaultValue": "false"
    },
    "hidden": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "If `true` the steps navigation is hidden."
      },
      "attribute": "hidden",
      "reflect": false,
      "defaultValue": "false"
    },
    "hasBack": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "If `true` the steps navigation has back button."
      },
      "attribute": "has-back",
      "reflect": false,
      "defaultValue": "false"
    },
    "navigation": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "If `true` the navigation is handled by the component"
      },
      "attribute": "navigation",
      "reflect": false,
      "defaultValue": "false"
    },
    "backLabel": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Label for back button"
      },
      "attribute": "back-label",
      "reflect": false,
      "defaultValue": "''"
    },
    "showLabel": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Hides the navigation circles and adds the step label instead"
      },
      "attribute": "show-label",
      "reflect": false,
      "defaultValue": "false"
    }
  }; }
  static get states() { return {
    "stepOptions": {},
    "label": {}
  }; }
  static get events() { return [{
      "method": "balChange",
      "name": "balCardStepChange",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": "Emitted when the changes has finished."
      },
      "complexType": {
        "original": "BalCardStepOption",
        "resolved": "BalCardStepOption",
        "references": {
          "BalCardStepOption": {
            "location": "import",
            "path": "../bal-card-step/bal-card-step.type"
          }
        }
      }
    }, {
      "method": "balBackClick",
      "name": "balBackClick",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": "Emitted when the back button is clicked."
      },
      "complexType": {
        "original": "void",
        "resolved": "void",
        "references": {}
      }
    }, {
      "method": "balStepClick",
      "name": "balCardStepClick",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": "Emitted when the step circle is clicked."
      },
      "complexType": {
        "original": "BalCardStepOption",
        "resolved": "BalCardStepOption",
        "references": {
          "BalCardStepOption": {
            "location": "import",
            "path": "../bal-card-step/bal-card-step.type"
          }
        }
      }
    }]; }
  static get methods() { return {
    "select": {
      "complexType": {
        "signature": "(step: BalCardStepOption) => Promise<void>",
        "parameters": [{
            "tags": [],
            "text": ""
          }],
        "references": {
          "Promise": {
            "location": "global"
          },
          "BalCardStepOption": {
            "location": "import",
            "path": "../bal-card-step/bal-card-step.type"
          }
        },
        "return": "Promise<void>"
      },
      "docs": {
        "text": "Go to tab with the given value",
        "tags": []
      }
    },
    "sync": {
      "complexType": {
        "signature": "() => Promise<void>",
        "parameters": [],
        "references": {
          "Promise": {
            "location": "global"
          }
        },
        "return": "Promise<void>"
      },
      "docs": {
        "text": "",
        "tags": []
      }
    }
  }; }
  static get elementRef() { return "element"; }
}
