import { proxyCustomElement, HTMLElement, createEvent, h } from '@stencil/core/internal/client';
import { u as updateOrder } from './index3.js';
import { d as defineCustomElement$1 } from './sc-line-items-provider2.js';

const ScCartSessionProvider = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.__attachShadow();
    this.scUpdateOrderState = createEvent(this, "scUpdateOrderState", 7);
    this.scError = createEvent(this, "scError", 7);
    this.scSetState = createEvent(this, "scSetState", 7);
  }
  /** Sync this session back to parent. */
  handleSessionUpdate(val) {
    this.scUpdateOrderState.emit(val);
  }
  handleUpdateSession(e) {
    const { data, options } = e.detail;
    if (options === null || options === void 0 ? void 0 : options.silent) {
      this.update(data);
    }
    else {
      this.loadUpdate(data);
    }
  }
  /** Handles coupon updates. */
  async handleCouponApply(e) {
    const promotion_code = e.detail;
    this.scError.emit({});
    this.loadUpdate({
      discount: {
        ...(promotion_code ? { promotion_code } : {}),
      },
    });
  }
  /** Handle the error response. */
  handleErrorResponse(e) {
    if ((e === null || e === void 0 ? void 0 : e.code) === 'readonly') {
      this.scUpdateOrderState.emit(null);
    }
    // expired
    if ((e === null || e === void 0 ? void 0 : e.code) === 'rest_cookie_invalid_nonce') {
      this.scSetState.emit('idle');
      return;
    }
    // something went wrong
    if (e === null || e === void 0 ? void 0 : e.message) {
      this.scError.emit(e);
    }
    // handle curl timeout errors.
    if ((e === null || e === void 0 ? void 0 : e.code) === 'http_request_failed') {
      this.scError.emit({ message: 'Something went wrong. Please reload the page and try again.' });
    }
    this.scSetState.emit('idle');
  }
  /** Fetch a session. */
  async fetch(args = {}) {
    this.loadUpdate({ status: 'draft', ...args });
  }
  /** Update a the order */
  async update(data = {}, query = {}) {
    var _a;
    try {
      this.session = (await updateOrder({
        id: (_a = this.order) === null || _a === void 0 ? void 0 : _a.id,
        data: {
          ...data,
        },
        query: {
          ...query,
        },
      }));
    }
    catch (e) {
      console.error(e);
      throw e;
    }
  }
  /** Updates a session with loading status changes. */
  async loadUpdate(data = {}) {
    try {
      this.scSetState.emit('busy');
      await this.update(data);
      this.scSetState.emit('idle');
    }
    catch (e) {
      this.handleErrorResponse(e);
    }
  }
  render() {
    return (h("sc-line-items-provider", { order: this.order, onScUpdateLineItems: e => this.loadUpdate({ line_items: e.detail }) }, h("slot", null)));
  }
  get el() { return this; }
  static get watchers() { return {
    "session": ["handleSessionUpdate"]
  }; }
}, [1, "sc-cart-session-provider", {
    "order": [16],
    "session": [32]
  }, [[0, "scUpdateOrder", "handleUpdateSession"], [0, "scApplyCoupon", "handleCouponApply"]]]);
function defineCustomElement() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["sc-cart-session-provider", "sc-line-items-provider"];
  components.forEach(tagName => { switch (tagName) {
    case "sc-cart-session-provider":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, ScCartSessionProvider);
      }
      break;
    case "sc-line-items-provider":
      if (!customElements.get(tagName)) {
        defineCustomElement$1();
      }
      break;
  } });
}
defineCustomElement();

export { ScCartSessionProvider as S, defineCustomElement as d };
