import { Component, Element, Event, h, Prop, Watch } from '@stencil/core';
import { __ } from '@wordpress/i18n';
import { openWormhole } from 'stencil-wormhole';
import { animateTo, shimKeyframesHeightAuto, stopAnimations } from '../../../../functions/animate';
import { getAnimation, setDefaultAnimation } from '../../../../functions/animation-registry';
export class ScOrderSummary {
  constructor() {
    this.closedText = __('Show Summary', 'surecart');
    this.openText = __('Summary', 'surecart');
    this.collapsible = false;
  }
  componentDidLoad() {
    this.body.hidden = this.collapsed;
    this.body.style.height = !this.collapsed ? 'auto' : '0';
  }
  handleClick(e) {
    e.preventDefault();
    if (this.empty && !this.loading)
      return;
    this.collapsed = !this.collapsed;
  }
  renderHeader() {
    var _a;
    // loading state
    if (this.loading && !((_a = this.order) === null || _a === void 0 ? void 0 : _a.id)) {
      return (h("sc-line-item", null,
        h("sc-skeleton", { slot: "title", style: { width: '120px', display: 'inline-block' } }),
        h("sc-skeleton", { slot: "price", style: { 'width': '70px', 'display': 'inline-block', '--border-radius': '6px' } }),
        h("sc-skeleton", { slot: "currency", style: { width: '30px', display: 'inline-block' } })));
    }
    return (h("sc-line-item", { style: { '--price-size': 'var(--sc-font-size-x-large)' } },
      h("span", { class: "collapse-link", slot: "title", onClick: e => this.handleClick(e) },
        this.collapsed ? this.closedText : this.openText,
        h("svg", { xmlns: "http://www.w3.org/2000/svg", class: "collapse-link__icon", fill: "none", viewBox: "0 0 24 24", stroke: "currentColor" },
          h("path", { "stroke-linecap": "round", "stroke-linejoin": "round", "stroke-width": "2", d: "M19 9l-7 7-7-7" }))),
      h("span", { slot: "description" },
        h("slot", { name: "description" })),
      h("span", { slot: "price" },
        h("sc-total", { total: 'total', order: this.order }))));
  }
  async handleOpenChange() {
    if (!this.collapsed) {
      this.scShow.emit();
      await stopAnimations(this.body);
      this.body.hidden = false;
      const { keyframes, options } = getAnimation(this.el, 'summary.show');
      await animateTo(this.body, shimKeyframesHeightAuto(keyframes, this.body.scrollHeight), options);
      this.body.style.height = 'auto';
    }
    else {
      this.scHide.emit();
      await stopAnimations(this.body);
      const { keyframes, options } = getAnimation(this.el, 'summary.hide');
      await animateTo(this.body, shimKeyframesHeightAuto(keyframes, this.body.scrollHeight), options);
      this.body.hidden = true;
      this.body.style.height = 'auto';
    }
  }
  render() {
    return (h("div", { class: { 'summary': true, 'summary--open': !this.collapsed } },
      this.collapsible && this.renderHeader(),
      h("div", { ref: el => (this.body = el), class: {
          'summary__content': true,
          'summary__content--empty': this.empty && !this.loading,
        } },
        h("slot", null)),
      this.empty && !this.loading && h("p", { class: "empty" }, __('Your cart is empty.', 'surecart'))));
  }
  static get is() { return "sc-order-summary"; }
  static get encapsulation() { return "shadow"; }
  static get originalStyleUrls() { return {
    "$": ["sc-order-summary.scss"]
  }; }
  static get styleUrls() { return {
    "$": ["sc-order-summary.css"]
  }; }
  static get properties() { return {
    "order": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Order",
        "resolved": "Order",
        "references": {
          "Order": {
            "location": "import",
            "path": "../../../../types"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "loading": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "loading",
      "reflect": false
    },
    "empty": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "empty",
      "reflect": false
    },
    "closedText": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "closed-text",
      "reflect": false,
      "defaultValue": "__('Show Summary', 'surecart')"
    },
    "openText": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "open-text",
      "reflect": false,
      "defaultValue": "__('Summary', 'surecart')"
    },
    "collapsible": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "collapsible",
      "reflect": false,
      "defaultValue": "false"
    },
    "collapsed": {
      "type": "boolean",
      "mutable": true,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "collapsed",
      "reflect": false
    }
  }; }
  static get events() { return [{
      "method": "scShow",
      "name": "scShow",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": "Show the toggle"
      },
      "complexType": {
        "original": "void",
        "resolved": "void",
        "references": {}
      }
    }, {
      "method": "scHide",
      "name": "scHide",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": "Show the toggle"
      },
      "complexType": {
        "original": "void",
        "resolved": "void",
        "references": {}
      }
    }]; }
  static get elementRef() { return "el"; }
  static get watchers() { return [{
      "propName": "collapsed",
      "methodName": "handleOpenChange"
    }]; }
}
setDefaultAnimation('summary.show', {
  keyframes: [
    { height: '0', opacity: '0' },
    { height: 'auto', opacity: '1' },
  ],
  options: { duration: 250, easing: 'ease' },
});
setDefaultAnimation('summary.hide', {
  keyframes: [
    { height: 'auto', opacity: '1' },
    { height: '0', opacity: '0' },
  ],
  options: { duration: 250, easing: 'ease' },
});
openWormhole(ScOrderSummary, ['order', 'loading', 'empty'], false);
