import { proxyCustomElement, HTMLElement, h, Host } from '@stencil/core/internal/client';
import { s as state } from './watchers3.js';
import { c as currentCheckout } from './getters2.js';

/**
 * Check if any of the rule groups is passed or not.
 *
 * @param {array} groups Rule groups.
 * @param {object} props Data.
 * @returns {boolean}
 */
const hasAnyRuleGroupPassed = (groups, props) => {
  return (groups || []).some(({ rules }) => hasRulesPassed(rules, props));
};
/**
 * CHeck if all rules are passed or not.
 *
 * @param {array} rules Rules.
 * @param {object} props Data.
 * @returns {boolean}
 */
const hasRulesPassed = (rules, { checkout, processor }) => {
  return rules
    .map(rule => {
    var _a, _b;
    const ruleValue = Array.isArray(rule === null || rule === void 0 ? void 0 : rule.value) ? (rule === null || rule === void 0 ? void 0 : rule.value).map(ruleValue => (ruleValue === null || ruleValue === void 0 ? void 0 : ruleValue.value) || ruleValue) : rule === null || rule === void 0 ? void 0 : rule.value;
    switch (rule === null || rule === void 0 ? void 0 : rule.condition) {
      case 'total':
        return compareNumberValues(parseFloat(checkout.total_amount), parseFloat(ruleValue), rule === null || rule === void 0 ? void 0 : rule.operator);
      case 'products':
        return compareObjectValues(getCartProductIds(checkout), ruleValue, rule === null || rule === void 0 ? void 0 : rule.operator);
      case 'coupons':
        return compareObjectValues(getCartCouponIds(checkout), ruleValue, rule === null || rule === void 0 ? void 0 : rule.operator);
      case 'shipping_country':
        return compareObjectValues([(_a = checkout === null || checkout === void 0 ? void 0 : checkout.shipping_address) === null || _a === void 0 ? void 0 : _a.country], ruleValue, rule === null || rule === void 0 ? void 0 : rule.operator);
      case 'billing_country':
        return compareObjectValues([(_b = checkout === null || checkout === void 0 ? void 0 : checkout.billing_address) === null || _b === void 0 ? void 0 : _b.country], ruleValue, rule === null || rule === void 0 ? void 0 : rule.operator);
      case 'processors':
        return compareObjectValues([processor], ruleValue, rule === null || rule === void 0 ? void 0 : rule.operator);
      default:
        return false;
    }
  })
    .every(rules => rules);
};
/**
 * Get array of products from checkout.
 *
 * @param {object} checkout CHeckout data.
 * @returns {array}
 */
const getCartProductIds = (checkout) => {
  var _a;
  return (((_a = checkout === null || checkout === void 0 ? void 0 : checkout.line_items) === null || _a === void 0 ? void 0 : _a.data) || []).map(({ price }) => { var _a; return (_a = price === null || price === void 0 ? void 0 : price.product) === null || _a === void 0 ? void 0 : _a.id; });
};
/**
 * Get array of coupons from checkout.
 *
 * @param {object} checkout CHeckout data.
 * @returns {array}
 */
const getCartCouponIds = (checkout) => {
  var _a, _b, _c, _d;
  return ((_b = (_a = checkout === null || checkout === void 0 ? void 0 : checkout.discount) === null || _a === void 0 ? void 0 : _a.coupon) === null || _b === void 0 ? void 0 : _b.id) ? [(_d = (_c = checkout === null || checkout === void 0 ? void 0 : checkout.discount) === null || _c === void 0 ? void 0 : _c.coupon) === null || _d === void 0 ? void 0 : _d.id] : [];
};
/**
 * Compare object values.
 *
 * @param {array} cart_values order values.
 * @param {array} rule_values rules values.
 * @param {string} operator rule operator.
 * @returns {boolean}
 */
const compareObjectValues = (cart_values, rule_values, operator) => {
  switch (operator) {
    case 'all':
      return rule_values.filter(n1 => cart_values.some(n2 => n1 == n2)).length === rule_values.length;
    case 'any':
      return cart_values.filter(n1 => rule_values.some(n2 => n1 == n2)).length >= 1;
    case 'none':
      return cart_values.filter(n1 => rule_values.some(n2 => n1 == n2)).length === 0;
    case 'exist':
      return cart_values.length >= 1;
    case 'not_exist':
      return cart_values.length === 0;
    default:
      return false;
  }
};
/**
 * Compare string values.
 *
 * @param string number1 The actual number from cart/order.
 * @param array  number2 Rule values.
 * @param string operator Rule operator.
 * @returns {boolean}
 */
const compareNumberValues = (number1, number2, operator) => {
  switch (operator) {
    case '==':
      return number1 === number2;
    case '!=':
      return number1 !== number2;
    case '>':
      return number1 > number2;
    case '<':
      return number1 < number2;
    case '<=':
      return number1 <= number2;
    case '>=':
      return number1 >= number2;
  }
  return false;
};

const scConditionalFormCss = ":host{display:block}";

const ScConditionalForm$1 = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.__attachShadow();
  }
  render() {
    let show = hasAnyRuleGroupPassed(this.rule_groups, { checkout: currentCheckout(), processor: state === null || state === void 0 ? void 0 : state.id });
    if (!show)
      return null;
    return (h(Host, null, h("slot", null)));
  }
  static get style() { return scConditionalFormCss; }
}, [1, "sc-conditional-form", {
    "rule_groups": [16]
  }]);
function defineCustomElement$1() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["sc-conditional-form"];
  components.forEach(tagName => { switch (tagName) {
    case "sc-conditional-form":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, ScConditionalForm$1);
      }
      break;
  } });
}
defineCustomElement$1();

const ScConditionalForm = ScConditionalForm$1;
const defineCustomElement = defineCustomElement$1;

export { ScConditionalForm, defineCustomElement };
