{"version":3,"file":"answers_controller-D1TYOT_C.js","sources":["../../../app/javascript/controllers/automation/answers_controller.js"],"sourcesContent":["import GroupController from '../filter/group_controller'\n\nexport default class extends GroupController {\n static values = {\n answers: Array,\n stepName: String,\n delivery: String,\n }\n\n static targets = [\n 'submit',\n 'limitNotice',\n 'details',\n 'openModal',\n 'closeModal',\n 'gracePeriodInput',\n 'removeButton',\n 'stepNameInput'\n ]\n\n connect() {\n this.delivery = this.deliveryValue\n\n this.detailsTarget.addEventListener('toggle', ({ target }) => {\n if(target.open) {\n const lastItem = this.itemTargets[this.itemTargets.length - 1]\n\n const input = lastItem.querySelector('input')\n this.focus(input, { moveCursorToEnd: true })\n }\n })\n\n super.connect()\n }\n\n update({ detail: answersWithIds }) {\n if(!Array.isArray(answersWithIds)) {\n return\n }\n\n this.itemTargets.forEach((item, index) => {\n item.setAttribute('data-id', answersWithIds[index].id)\n })\n\n this.answersValue = answersWithIds\n }\n\n onInputEnter({ target }) {\n if(!target.value || this.limitReached) return\n\n const parent = target.closest('[data-automation--answers-target=\"item\"]')\n const lastItem = this.itemTargets[this.itemTargets.length - 1]\n\n if(parent === lastItem) {\n this.addNewItem()\n }\n }\n\n onAnswerChange({ detail, srcElement }) {\n const { id, ...answer } = detail\n const elementIndex = this.itemTargets.indexOf(srcElement)\n\n if(this.answersValue.length === 0) {\n return this.answersValue = [\n {\n id,\n ...answer\n }\n ]\n }\n\n this.answersValue = this.answersValue.map((value, index) => {\n // when answer is new and has not been saved yet, the ID is null\n // when the id is null, we compare by the index of the element.\n if((id && value.id === id) || index === elementIndex) {\n return {\n id,\n ...answer\n }\n } else {\n return value\n }\n })\n }\n\n onComposeInitialization({ detail: composeSnapshot }) {\n this.nextTick(() => {\n this.dispatch('compose:initialized', {\n detail: composeSnapshot,\n target: this.stepElement,\n })\n }, 1000)\n }\n\n onComposeChange({ detail: composeSnapshot }) {\n this.nextTick(() => {\n this.dispatch('compose:changed', {\n detail: composeSnapshot,\n target: this.stepElement,\n })\n })\n }\n\n onClickOutside({ target }) {\n if(this.paneIsInvisible || this.element.contains(target) || target.closest('dialog')) return\n\n if(this.hasUnsavedChanges) {\n return this.openModalTarget.click()\n }\n\n this.abortChanges()\n }\n\n abortChanges() {\n this.dispatch('abort', {\n target: this.stepElement,\n })\n }\n\n onGracePeriodIntervalChange({ detail }) {\n this.dispatch('gracePeriodInterval:changed', {\n target: this.stepElement,\n detail,\n })\n\n this.focus(this.gracePeriodInputTarget, { moveCursorToEnd: true })\n }\n\n onDeliveryChange({ detail: delivery }) {\n this.delivery = delivery\n }\n\n onModalClose() {\n const lastItem = this.itemTargets[this.itemTargets.length - 1]\n\n const input = lastItem.querySelector('input')\n this.focus(input, { moveCursorToEnd: true })\n }\n\n showAndFocusAnswer({ detail: index }) {\n const item = this.itemTargets[index]\n\n item.scrollIntoView({ behaviour: 'instant' })\n\n this.dispatch('focus', {\n target: item,\n })\n }\n\n addNewItem() {\n if(this.limitReached) return\n\n const lastItem = this.itemTargets[this.itemTargets.length - 1]\n const newItem = this.templateTarget.cloneNode(true)\n\n newItem.querySelector('label').textContent = t.automation.answer.label.replace(\"%{count}\", this.itemTargets.length + 1)\n\n newItem.setAttribute('data-automation--answers-target', 'item')\n newItem.setAttribute('data-controller', 'automation--answer-field')\n\n newItem.classList.remove('hidden')\n newItem.querySelectorAll('input, select').forEach(this.enable)\n\n this.listTarget.insertBefore(newItem, this.templateTarget)\n\n this.nextTick(() => {\n this.focus(newItem.querySelector('input'))\n })\n\n lastItem.querySelector(\"[data-filter--group-item-target='removeButtonContainer']\").classList.remove('hidden')\n\n this.answersValue = [\n ...this.answersValue,\n {\n body: \"\",\n query: 'equals',\n text: t.applied_filters.query.equals,\n }\n ]\n }\n\n removeItem({ currentTarget }) {\n const item = currentTarget.closest('[data-automation--answers-target=\"item\"]')\n\n this.answersValue = this.answersValue.filter((_, index) => {\n return index !== this.itemTargets.indexOf(item)\n })\n\n item.remove()\n }\n\n itemTargetDisconnected() {\n super.itemTargetDisconnected()\n\n this.itemTargets.forEach((item, index) => {\n item.querySelector('label').textContent = t.automation.answer.label.replace(\"%{count}\", index + 1)\n })\n\n this.hide(this.limitNoticeTarget)\n }\n\n itemTargetConnected() {\n if(this.limitReached) {\n this.hide(this.orValueButtonTarget)\n this.show(this.limitNoticeTarget)\n } else {\n this.hide(this.limitNoticeTarget)\n }\n\n if(this.itemTargets.length === 1) {\n this.itemTargets[0].querySelector(\"[data-filter--group-item-target='removeButtonContainer']\").classList.add('hidden')\n } else {\n this.itemTargets.forEach(item => {\n item.querySelector(\"[data-filter--group-item-target='removeButtonContainer']\").classList.remove('hidden')\n })\n }\n }\n\n answersValueChanged() {\n const valid = !(this.answersValue.length === 0 || (this.answersValue.length === 1 && this.answersValue[0].body === \"\"))\n\n this.nextTick(() => {\n this.dispatch('validation', {\n target: this.stepElement,\n detail: {\n valid,\n answers: this.answersValue,\n }\n })\n }, 0)\n }\n\n save() {\n if(this.hasDuplicateAnswers) {\n this.showToast({\n message: t.automation.answer.duplicates_toast,\n })\n\n return\n }\n\n this.stepNameValue = this.stepNameInputTarget.value.trim()\n this.deliveryValue = this.delivery\n\n this.stepController.save()\n this.stepController.stepNameTarget.innerHTML = this.stepNameValue || t.journeys.new.question\n\n this.show(this.removeButtonTarget)\n }\n\n get hasDuplicateAnswers() {\n const groupedByBody = this.answersValue.reduce((acc, item) => {\n if (!acc[item.body]) {\n acc[item.body] = [];\n }\n acc[item.body].push(item);\n return acc;\n }, {});\n\n return Object.entries(groupedByBody)\n .filter(([_, items]) => items.length > 1)\n .map(([body, items]) => ({ body, items }))\n .length > 0\n }\n\n remove() {\n this.stepController.remove()\n }\n\n get stepElement() {\n return document.getElementById(this.element.id.replace('_pane', ''))\n }\n\n get hasUnsavedChanges() {\n return this.stepController.hasUnsavedChanges\n || this.stepNameInputTarget.value.trim() !== this.stepNameValue\n || this.delivery !== this.deliveryValue\n }\n\n get stepController() {\n return this.application.getControllerForElementAndIdentifier(this.stepElement, 'automation--question')\n }\n\n get paneIsInvisible() {\n return this.element.classList.contains(\"-right-full\")\n }\n\n get paneIsVisible() {\n return this.element.classList.contains(\"right-0\")\n }\n}\n"],"names":["answers_controller","GroupController","target","input","answersWithIds","item","index","parent","lastItem","detail","srcElement","id","answer","elementIndex","value","composeSnapshot","delivery","newItem","currentTarget","_","valid","groupedByBody","acc","items","body","__publicField"],"mappings":"uNAEe,MAAKA,UAASC,CAAgB,CAkB3C,SAAU,CACR,KAAK,SAAW,KAAK,cAErB,KAAK,cAAc,iBAAiB,SAAU,CAAC,CAAE,OAAAC,CAAM,IAAO,CAC5D,GAAGA,EAAO,KAAM,CAGd,MAAMC,EAFW,KAAK,YAAY,KAAK,YAAY,OAAS,CAAC,EAEtC,cAAc,OAAO,EAC5C,KAAK,MAAMA,EAAO,CAAE,gBAAiB,EAAM,CAAA,CACnD,CACK,CAAA,EAED,MAAM,QAAO,CACjB,CAEE,OAAO,CAAE,OAAQC,GAAkB,CAC7B,MAAM,QAAQA,CAAc,IAIhC,KAAK,YAAY,QAAQ,CAACC,EAAMC,IAAU,CACxCD,EAAK,aAAa,UAAWD,EAAeE,CAAK,EAAE,EAAE,CACtD,CAAA,EAED,KAAK,aAAeF,EACxB,CAEE,aAAa,CAAE,OAAAF,GAAU,CACvB,GAAG,CAACA,EAAO,OAAS,KAAK,aAAc,OAEvC,MAAMK,EAASL,EAAO,QAAQ,0CAA0C,EAClEM,EAAW,KAAK,YAAY,KAAK,YAAY,OAAS,CAAC,EAE1DD,IAAWC,GACZ,KAAK,WAAU,CAErB,CAEE,eAAe,CAAE,OAAAC,EAAQ,WAAAC,GAAc,CACrC,KAAM,CAAE,GAAAC,EAAI,GAAGC,GAAWH,EACpBI,EAAe,KAAK,YAAY,QAAQH,CAAU,EAExD,GAAG,KAAK,aAAa,SAAW,EAC9B,OAAO,KAAK,aAAe,CACzB,CACE,GAAAC,EACA,GAAGC,CACb,CACA,EAGI,KAAK,aAAe,KAAK,aAAa,IAAI,CAACE,EAAOR,IAG5CK,GA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