import { Controller } from "@hotwired/stimulus"; import TomSelect from "tom-select"; function _checkPrivateRedeclaration(e, t) { if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object"); } function _classPrivateMethodInitSpec(e, a) { _checkPrivateRedeclaration(e, a), a.add(e); } function _classPrivateFieldInitSpec(e, t, a) { _checkPrivateRedeclaration(e, t), t.set(e, a); } function _assertClassBrand(e, t, n) { if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n; throw new TypeError("Private element is not present on this object"); } function _classPrivateFieldGet2(s, a) { return s.get(_assertClassBrand(s, a)); } var _Class_brand = /* @__PURE__ */ new WeakSet(); var _normalizePluginsToHash = /* @__PURE__ */ new WeakMap(); var _Class = class extends Controller { constructor(..._args) { super(..._args); _classPrivateMethodInitSpec(this, _Class_brand); this.isObserving = false; this.hasLoadedChoicesPreviously = false; this.originalOptions = []; _classPrivateFieldInitSpec(this, _normalizePluginsToHash, (plugins) => { if (Array.isArray(plugins)) return plugins.reduce((acc, plugin) => { if (typeof plugin === "string") acc[plugin] = {}; if (typeof plugin === "object" && plugin.name) acc[plugin.name] = plugin.options || {}; return acc; }, {}); return plugins; }); } initialize() { if (!this.mutationObserver) this.mutationObserver = new MutationObserver((mutations) => { this.onMutations(mutations); }); } connect() { if (this.selectElement) this.originalOptions = this.createOptionsDataStructure(this.selectElement); this.initializeTomSelect(); } initializeTomSelect() { if (this.selectElement) this.selectElement.setAttribute("data-skip-morph", ""); if (this.urlValue) { this.tomSelect = _assertClassBrand(_Class_brand, this, _createAutocompleteWithRemoteData).call(this, this.urlValue, this.hasMinCharactersValue ? this.minCharactersValue : null); return; } if (this.optionsAsHtmlValue) { this.tomSelect = _assertClassBrand(_Class_brand, this, _createAutocompleteWithHtmlContents).call(this); return; } this.tomSelect = _assertClassBrand(_Class_brand, this, _createAutocomplete).call(this); this.startMutationObserver(); } disconnect() { this.stopMutationObserver(); if (!this.tomSelect) return; let currentSelectedValues = []; if (this.selectElement) if (this.selectElement.multiple) currentSelectedValues = Array.from(this.selectElement.options).filter((option) => option.selected).map((option) => option.value); else currentSelectedValues = [this.selectElement.value]; this.tomSelect.destroy(); this.tomSelect = void 0; if (this.selectElement) if (this.selectElement.multiple) Array.from(this.selectElement.options).forEach((option) => { option.selected = currentSelectedValues.includes(option.value); }); else this.selectElement.value = currentSelectedValues[0]; } urlValueChanged() { this.resetTomSelect(); } getMaxOptions() { return this.selectElement ? this.selectElement.options.length : 50; } get selectElement() { if (!(this.element instanceof HTMLSelectElement)) return null; return this.element; } get formElement() { if (!(this.element instanceof HTMLInputElement) && !(this.element instanceof HTMLSelectElement)) throw new Error("Autocomplete Stimulus controller can only be used on an or