import { Component, Element, Event, h, Host, Listen, Method, Prop, State } from '@stencil/core';
export class CardSteps {
  constructor() {
    this.stepOptions = [];
    /**
     * 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;
    /**
     * Label for back button
     */
    this.backLabel = '';
  }
  /**
   * Select a step.
   */
  async select(value) {
    this.steps.forEach((t) => t.setActive(t.value === value));
    this.readSteps();
  }
  componentWillLoad() {
    this.readSteps();
  }
  stepChanged() {
    this.readSteps();
  }
  get steps() {
    return Array.from(this.element.querySelectorAll('bal-card-step'));
  }
  readSteps() {
    Promise.all(this.steps.map((value) => value.getOptions())).then((stepOptions) => {
      this.stepOptions = stepOptions;
    });
  }
  async onSelectStep(step) {
    if (!step.disabled) {
      await this.select(step.value);
      this.balChange.emit(step);
    }
    this.balStepClick.emit(step);
  }
  getPreviousStepIndex() {
    let activeStepIndex = this.stepOptions.findIndex(el => el.active === true);
    return activeStepIndex - 1;
  }
  async onbackButton() {
    let previousStepIndex = this.getPreviousStepIndex();
    let previousStep = this.stepOptions[previousStepIndex];
    if (previousStep && !previousStep.disabled && previousStepIndex >= 0) {
      await this.select(previousStep.value);
    }
    this.balBackClick.emit();
  }
  render() {
    return (h(Host, null,
      h("div", { class: "card-steps-wrapper" },
        h("div", { class: ['left-side', !this.hasBack ? 'is-hidden' : ''].join(' ') },
          h("a", { role: "button", onClick: () => this.onbackButton() },
            h("bal-icon", { class: "nav-go-left", name: "nav-go-large", size: "large", color: "white" })),
          h("span", { class: "nav-go-left-label" }, this.backLabel)),
        h("div", { class: ['tabs', this.inverted ? 'is-inverted' : '', this.hidden ? 'is-hidden' : ''].join(' ') },
          h("ul", null, this.stepOptions.filter(step => !step.hidden && !this.hidden).map((step, index) => (h("li", { class: [
              step.active ? 'is-active' : '',
              step.disabled ? 'is-disabled' : '',
              step.done ? 'is-done' : '',
            ].join(' ') },
            h("a", { onClick: () => this.onSelectStep(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"
    },
    "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": "''"
    }
  }; }
  static get states() { return {
    "stepOptions": {}
  }; }
  static get events() { return [{
      "method": "balChange",
      "name": "balChange",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": "Emitted when the changes has finished."
      },
      "complexType": {
        "original": "BalCardStepOptions",
        "resolved": "BalCardStepOptions",
        "references": {
          "BalCardStepOptions": {
            "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": "balStepClick",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": "Emitted when the step circle is clicked."
      },
      "complexType": {
        "original": "BalCardStepOptions",
        "resolved": "BalCardStepOptions",
        "references": {
          "BalCardStepOptions": {
            "location": "import",
            "path": "../bal-card-step/bal-card-step.type"
          }
        }
      }
    }]; }
  static get methods() { return {
    "select": {
      "complexType": {
        "signature": "(value: string) => Promise<void>",
        "parameters": [{
            "tags": [],
            "text": ""
          }],
        "references": {
          "Promise": {
            "location": "global"
          }
        },
        "return": "Promise<void>"
      },
      "docs": {
        "text": "Select a step.",
        "tags": []
      }
    }
  }; }
  static get elementRef() { return "element"; }
  static get listeners() { return [{
      "name": "balCardStepChanged",
      "method": "stepChanged",
      "target": undefined,
      "capture": false,
      "passive": false
    }]; }
}
