import { s as M, x as D, r as L, i as O, e as w } from "./query-assigned-elements-D930kELM.mjs";
import { M as E, a as A, b as T, C as _, c as v } from "./chip-S-9zvyg8.mjs";
import { a as B, b as F, c as I } from "./tslib.es6-pPsSiLLK.mjs";
import { M as P } from "./foundation-PL3XY-m_.mjs";
import { K as u } from "./keyboard-BLpVBEDM.mjs";
import { n as y } from "./property-DJzm_oB9.mjs";
import { i as U } from "./query-CDOd1Kwm.mjs";
/**
 * @license
 * Copyright 2016 Google Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
var N;
(function(a) {
  a.ARIA_MULTISELECTABLE = "aria-multiselectable";
})(N || (N = {}));
var S;
(function(a) {
  a.CHIP = "mdc-evolution-chip";
})(S || (S = {}));
var f;
(function(a) {
  a.INTERACTION = "MDCChipSet:interaction", a.REMOVAL = "MDCChipSet:removal", a.SELECTION = "MDCChipSet:selection";
})(f || (f = {}));
/**
 * @license
 * Copyright 2020 Google Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
var d;
(function(a) {
  a[a.INCREMENT = 0] = "INCREMENT", a[a.DECREMENT = 1] = "DECREMENT";
})(d || (d = {}));
var k = (
  /** @class */
  function(a) {
    B(i, a);
    function i(t) {
      return a.call(this, F(F({}, i.defaultAdapter), t)) || this;
    }
    return Object.defineProperty(i, "defaultAdapter", {
      get: function() {
        return {
          announceMessage: function() {
          },
          emitEvent: function() {
          },
          getAttribute: function() {
            return null;
          },
          getChipActionsAtIndex: function() {
            return [];
          },
          getChipCount: function() {
            return 0;
          },
          getChipIdAtIndex: function() {
            return "";
          },
          getChipIndexById: function() {
            return 0;
          },
          isChipFocusableAtIndex: function() {
            return !1;
          },
          isChipSelectableAtIndex: function() {
            return !1;
          },
          isChipSelectedAtIndex: function() {
            return !1;
          },
          removeChipAtIndex: function() {
          },
          setChipFocusAtIndex: function() {
          },
          setChipSelectedAtIndex: function() {
          },
          startChipAnimationAtIndex: function() {
          }
        };
      },
      enumerable: !1,
      configurable: !0
    }), i.prototype.handleChipAnimation = function(t) {
      var e = t.detail, r = e.chipID, n = e.animation, o = e.isComplete, h = e.addedAnnouncement, s = e.removedAnnouncement, p = this.adapter.getChipIndexById(r);
      if (n === E.EXIT && o) {
        s && this.adapter.announceMessage(s), this.removeAfterAnimation(p, r);
        return;
      }
      if (n === E.ENTER && o && h) {
        this.adapter.announceMessage(h);
        return;
      }
    }, i.prototype.handleChipInteraction = function(t) {
      var e = t.detail, r = e.source, n = e.chipID, o = e.isSelectable, h = e.isSelected, s = e.shouldRemove, p = this.adapter.getChipIndexById(n);
      if (s) {
        this.removeChip(p);
        return;
      }
      this.focusChip(p, r, A.FOCUSABLE), this.adapter.emitEvent(f.INTERACTION, {
        chipIndex: p,
        chipID: n
      }), o && this.setSelection(p, r, !h);
    }, i.prototype.handleChipNavigation = function(t) {
      var e = t.detail, r = e.chipID, n = e.key, o = e.isRTL, h = e.source, s = this.adapter.getChipIndexById(r), p = n === u.ARROW_RIGHT && !o || n === u.ARROW_LEFT && o;
      if (p) {
        this.focusNextChipFrom(s + 1);
        return;
      }
      var c = n === u.ARROW_LEFT && !o || n === u.ARROW_RIGHT && o;
      if (c) {
        this.focusPrevChipFrom(s - 1);
        return;
      }
      if (n === u.ARROW_DOWN) {
        this.focusNextChipFrom(s + 1, h);
        return;
      }
      if (n === u.ARROW_UP) {
        this.focusPrevChipFrom(s - 1, h);
        return;
      }
      if (n === u.HOME) {
        this.focusNextChipFrom(0, h);
        return;
      }
      if (n === u.END) {
        this.focusPrevChipFrom(this.adapter.getChipCount() - 1, h);
        return;
      }
    }, i.prototype.getSelectedChipIndexes = function() {
      for (var t, e, r = /* @__PURE__ */ new Set(), n = this.adapter.getChipCount(), o = 0; o < n; o++) {
        var h = this.adapter.getChipActionsAtIndex(o);
        try {
          for (var s = (t = void 0, I(h)), p = s.next(); !p.done; p = s.next()) {
            var c = p.value;
            this.adapter.isChipSelectedAtIndex(o, c) && r.add(o);
          }
        } catch (l) {
          t = { error: l };
        } finally {
          try {
            p && !p.done && (e = s.return) && e.call(s);
          } finally {
            if (t) throw t.error;
          }
        }
      }
      return r;
    }, i.prototype.setChipSelected = function(t, e, r) {
      this.adapter.isChipSelectableAtIndex(t, e) && this.setSelection(t, e, r);
    }, i.prototype.isChipSelected = function(t, e) {
      return this.adapter.isChipSelectedAtIndex(t, e);
    }, i.prototype.removeChip = function(t) {
      t >= this.adapter.getChipCount() || t < 0 || (this.adapter.startChipAnimationAtIndex(t, E.EXIT), this.adapter.emitEvent(f.REMOVAL, {
        chipID: this.adapter.getChipIdAtIndex(t),
        chipIndex: t,
        isComplete: !1
      }));
    }, i.prototype.addChip = function(t) {
      t >= this.adapter.getChipCount() || t < 0 || this.adapter.startChipAnimationAtIndex(t, E.ENTER);
    }, i.prototype.focusNextChipFrom = function(t, e) {
      for (var r = this.adapter.getChipCount(), n = t; n < r; n++) {
        var o = this.getFocusableAction(n, d.INCREMENT, e);
        if (o) {
          this.focusChip(n, o, A.FOCUSABLE_AND_FOCUSED);
          return;
        }
      }
    }, i.prototype.focusPrevChipFrom = function(t, e) {
      for (var r = t; r > -1; r--) {
        var n = this.getFocusableAction(r, d.DECREMENT, e);
        if (n) {
          this.focusChip(r, n, A.FOCUSABLE_AND_FOCUSED);
          return;
        }
      }
    }, i.prototype.getFocusableAction = function(t, e, r) {
      var n = this.adapter.getChipActionsAtIndex(t);
      return e === d.DECREMENT && n.reverse(), r ? this.getMatchingFocusableAction(t, n, r) : this.getFirstFocusableAction(t, n);
    }, i.prototype.getFirstFocusableAction = function(t, e) {
      var r, n;
      try {
        for (var o = I(e), h = o.next(); !h.done; h = o.next()) {
          var s = h.value;
          if (this.adapter.isChipFocusableAtIndex(t, s))
            return s;
        }
      } catch (p) {
        r = { error: p };
      } finally {
        try {
          h && !h.done && (n = o.return) && n.call(o);
        } finally {
          if (r) throw r.error;
        }
      }
      return null;
    }, i.prototype.getMatchingFocusableAction = function(t, e, r) {
      var n, o, h = null;
      try {
        for (var s = I(e), p = s.next(); !p.done; p = s.next()) {
          var c = p.value;
          if (this.adapter.isChipFocusableAtIndex(t, c) && (h = c), h === r)
            return h;
        }
      } catch (l) {
        n = { error: l };
      } finally {
        try {
          p && !p.done && (o = s.return) && o.call(s);
        } finally {
          if (n) throw n.error;
        }
      }
      return h;
    }, i.prototype.focusChip = function(t, e, r) {
      var n, o;
      this.adapter.setChipFocusAtIndex(t, e, r);
      for (var h = this.adapter.getChipCount(), s = 0; s < h; s++) {
        var p = this.adapter.getChipActionsAtIndex(s);
        try {
          for (var c = (n = void 0, I(p)), l = c.next(); !l.done; l = c.next()) {
            var m = l.value;
            m === e && s === t || this.adapter.setChipFocusAtIndex(s, m, A.NOT_FOCUSABLE);
          }
        } catch (b) {
          n = { error: b };
        } finally {
          try {
            l && !l.done && (o = c.return) && o.call(c);
          } finally {
            if (n) throw n.error;
          }
        }
      }
    }, i.prototype.supportsMultiSelect = function() {
      return this.adapter.getAttribute(N.ARIA_MULTISELECTABLE) === "true";
    }, i.prototype.setSelection = function(t, e, r) {
      var n, o;
      if (this.adapter.setChipSelectedAtIndex(t, e, r), this.adapter.emitEvent(f.SELECTION, {
        chipID: this.adapter.getChipIdAtIndex(t),
        chipIndex: t,
        isSelected: r
      }), !this.supportsMultiSelect())
        for (var h = this.adapter.getChipCount(), s = 0; s < h; s++) {
          var p = this.adapter.getChipActionsAtIndex(s);
          try {
            for (var c = (n = void 0, I(p)), l = c.next(); !l.done; l = c.next()) {
              var m = l.value;
              m === e && s === t || this.adapter.setChipSelectedAtIndex(s, m, !1);
            }
          } catch (b) {
            n = { error: b };
          } finally {
            try {
              l && !l.done && (o = c.return) && o.call(c);
            } finally {
              if (n) throw n.error;
            }
          }
        }
    }, i.prototype.removeAfterAnimation = function(t, e) {
      this.adapter.removeChipAtIndex(t), this.adapter.emitEvent(f.REMOVAL, {
        chipIndex: t,
        isComplete: !0,
        chipID: e
      });
      var r = this.adapter.getChipCount();
      r <= 0 || this.focusNearestFocusableAction(t);
    }, i.prototype.focusNearestFocusableAction = function(t) {
      for (var e = this.adapter.getChipCount(), r = t, n = t; r > -1 || n < e; ) {
        var o = this.getNearestFocusableAction(r, n, T.TRAILING);
        if (o) {
          this.focusChip(o.index, o.action, A.FOCUSABLE_AND_FOCUSED);
          return;
        }
        r--, n++;
      }
    }, i.prototype.getNearestFocusableAction = function(t, e, r) {
      var n = this.getFocusableAction(t, d.DECREMENT, r);
      if (n)
        return {
          index: t,
          action: n
        };
      if (e === t)
        return null;
      var o = this.getFocusableAction(e, d.INCREMENT, r);
      return o ? {
        index: e,
        action: o
      } : null;
    }, i;
  }(P)
);
/**
 * @license
 * Copyright 2020 Google Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
var R;
(function(a) {
  a.POLITE = "polite", a.ASSERTIVE = "assertive";
})(R || (R = {}));
var W = "data-mdc-dom-announce";
function $(a, i) {
  V.getInstance().say(a, i);
}
var V = (
  /** @class */
  function() {
    function a() {
      this.liveRegions = /* @__PURE__ */ new Map();
    }
    return a.getInstance = function() {
      return a.instance || (a.instance = new a()), a.instance;
    }, a.prototype.say = function(i, t) {
      var e, r, n = (e = t == null ? void 0 : t.priority) !== null && e !== void 0 ? e : R.POLITE, o = (r = t == null ? void 0 : t.ownerDocument) !== null && r !== void 0 ? r : document, h = this.getLiveRegion(n, o);
      h.textContent = "", setTimeout(function() {
        h.textContent = i, o.addEventListener("click", s);
      }, 1);
      function s() {
        h.textContent = "", o.removeEventListener("click", s);
      }
    }, a.prototype.getLiveRegion = function(i, t) {
      var e = this.liveRegions.get(t);
      e || (e = /* @__PURE__ */ new Map(), this.liveRegions.set(t, e));
      var r = e.get(i);
      if (r && t.body.contains(r))
        return r;
      var n = this.createLiveRegion(i, t);
      return e.set(i, n), n;
    }, a.prototype.createLiveRegion = function(i, t) {
      var e = t.createElement("div");
      return e.style.position = "absolute", e.style.top = "-9999px", e.style.left = "-9999px", e.style.height = "1px", e.style.overflow = "hidden", e.setAttribute("aria-atomic", "true"), e.setAttribute("aria-live", i), e.setAttribute(W, "true"), t.body.appendChild(e), e;
    }, a;
  }()
), G = Object.defineProperty, g = (a, i, t, e) => {
  for (var r = void 0, n = a.length - 1, o; n >= 0; n--)
    (o = a[n]) && (r = o(i, t, r) || r);
  return r && G(i, t, r), r;
};
class C extends M {
  constructor() {
    super(...arguments), this.selectable = !1, this.multi = !1, this.label = "", this.activeIndex = 0, this._previousActiveIndex = -1, this.chips = [];
  }
  render() {
    return D`<span
      class="mdc-evolution-chip-set"
      role="${this.selectable || this.multi ? "listbox" : "grid"}"
      aria-label="${this.label}"
      aria-multiselectable="${this.multi}"
    >
      <span class="mdc-evolution-chip-set__chips" role="presentation">
        <slot></slot>
      </span>
    </span>`;
  }
  async updated(i) {
    super.updated(i), await this.updateComplete, this.activeIndex != this._previousActiveIndex && this.mdcFoundation.setChipSelected(
      this.activeIndex,
      T.PRIMARY,
      !0
    );
  }
  firstUpdated(i) {
    var r;
    super.firstUpdated(i);
    const t = (r = this.shadowRoot) == null ? void 0 : r.querySelector("slot"), e = (t == null ? void 0 : t.assignedElements().filter((n) => n instanceof _)) ?? [];
    for (let n = 0; n < e.length; n++) {
      const o = e[n];
      this.chips.push(o);
    }
    this.mdcFoundation !== void 0 && this.mdcFoundation.destroy(), this.mdcFoundation = new k(this.createAdapter()), this.mdcFoundation.init();
  }
  connectedCallback() {
    super.connectedCallback(), this.addEventListener(v.ANIMATION, this._handleChipAnimation), this.addEventListener(
      v.INTERACTION,
      this._handleChipInteraction
    ), this.addEventListener(v.NAVIGATION, this._handleChipNavigation);
  }
  disconnectedCallback() {
    super.disconnectedCallback(), this.removeEventListener(
      v.ANIMATION,
      this._handleChipAnimation
    ), this.removeEventListener(
      v.INTERACTION,
      this._handleChipInteraction
    ), this.removeEventListener(
      v.NAVIGATION,
      this._handleChipNavigation
    );
  }
  createAdapter() {
    return {
      /** Announces the message via an aria-live region */
      announceMessage: $,
      /** Emits the given event with the given detail. */
      emitEvent: (i, t) => this.dispatchEvent(
        new CustomEvent(i, { bubbles: !0, detail: t })
      ),
      /** Returns the value for the given attribute, if it exists. */
      getAttribute: (i) => this.mdcRoot.getAttribute(i),
      /** Returns the actions provided by the child chip at the given index. */
      getChipActionsAtIndex: (i) => this.chips[i].getActions(),
      /** Returns the number of child chips. */
      getChipCount: () => this.chips.length,
      /** Returns the ID of the chip at the given index. */
      getChipIdAtIndex: (i) => this.chips[i].getElementID(),
      /** Returns the index of the child chip with the matching ID. */
      getChipIndexById: (i) => this.chips.findIndex((t) => t.getElementID() === i),
      /** Proxies to the MDCChip#isActionFocusable method. */
      isChipFocusableAtIndex: (i, t) => this.chips[i].isActionFocusable(t),
      /** Proxies to the MDCChip#isActionSelectable method. */
      isChipSelectableAtIndex: (i, t) => this.chips[i].isActionSelectable(t),
      /** Proxies to the MDCChip#isActionSelected method. */
      isChipSelectedAtIndex: (i, t) => this.chips[i].isActionSelected(t),
      /** Removes the chip at the given index. */
      removeChipAtIndex: (i) => this.chips[i].remove(),
      /** Proxies to the MDCChip#setActionFocus method. */
      setChipFocusAtIndex: (i, t, e) => this.chips[i].setActionFocus(t, e),
      /** Proxies to the MDCChip#setActionSelected method. */
      setChipSelectedAtIndex: (i, t, e) => this.chips[i].setActionSelected(t, e),
      /** Starts the chip animation at the given index. */
      startChipAnimationAtIndex: (i, t) => this.chips[i].startAnimation(t)
    };
  }
  _handleChipAnimation(i) {
    this.mdcFoundation.handleChipAnimation(i);
  }
  _handleChipInteraction(i) {
    this.mdcFoundation.handleChipInteraction(i);
  }
  _handleChipNavigation(i) {
    this.mdcFoundation.handleChipNavigation(i);
  }
}
g([
  U(".mdc-evolution-chip-set")
], C.prototype, "mdcRoot");
g([
  y({ type: Boolean, reflect: !0 })
], C.prototype, "selectable");
g([
  y({ type: Boolean, reflect: !0 })
], C.prototype, "multi");
g([
  y()
], C.prototype, "label");
g([
  y({ type: Number })
], C.prototype, "activeIndex");
const H = ".mdc-evolution-chip-set{display:flex}.mdc-evolution-chip-set:focus{outline:none}.mdc-evolution-chip-set__chips{display:flex;flex-flow:wrap;min-width:0}.mdc-evolution-chip-set--overflow .mdc-evolution-chip-set__chips{flex-flow:nowrap}.mdc-evolution-chip-set .mdc-evolution-chip-set__chips{margin-left:-8px;margin-right:0}[dir=rtl] .mdc-evolution-chip-set .mdc-evolution-chip-set__chips,.mdc-evolution-chip-set .mdc-evolution-chip-set__chips[dir=rtl]{margin-left:0;margin-right:-8px}.mdc-evolution-chip-set .mdc-evolution-chip{margin-left:8px;margin-right:0}[dir=rtl] .mdc-evolution-chip-set .mdc-evolution-chip,.mdc-evolution-chip-set .mdc-evolution-chip[dir=rtl]{margin-left:0;margin-right:8px}.mdc-evolution-chip-set .mdc-evolution-chip{margin-top:4px;margin-bottom:4px}::slotted(cv-chip){margin-left:8px;margin-right:0}[dir=rtl] ::slotted(cv-chip),::slotted(cv-chip[dir=rtl]){margin-left:0;margin-right:8px}::slotted(cv-chip){margin-top:4px;margin-bottom:4px}";
var j = Object.getOwnPropertyDescriptor, K = (a, i, t, e) => {
  for (var r = e > 1 ? void 0 : e ? j(i, t) : i, n = a.length - 1, o; n >= 0; n--)
    (o = a[n]) && (r = o(r) || r);
  return r;
};
let x = class extends C {
};
x.styles = [
  O`
      ${L(H)}
    `
];
x = K([
  w("cv-chip-set")
], x);
const tt = x;
export {
  x as CovalentChipSet,
  tt as default
};
