{"version":3,"file":"coordinator_controller-Cr92hic9.js","sources":["../../../app/javascript/controllers/compose/coordinator_controller.js"],"sourcesContent":["import ApplicationController from '../application_controller'\n\nexport default class extends ApplicationController {\n static values = {\n windows: Array,\n activeWindow: Object,\n technologies: Array,\n }\n\n static targets = [\n 'destinationMenu',\n 'sourceOption',\n 'destinationOption',\n 'destinationMenuTrigger',\n 'windowUpdate',\n 'refreshedDestinationMenu',\n 'templatesMenu',\n 'inputContainer',\n 'disableNotice'\n ]\n\n onClickOutside({target}) {\n if (this.isInvisible(this.destinationMenuTarget) || this.destinationMenuTarget.contains(target) || this.destinationMenuTriggerTarget.contains(target)) return\n this.element.setAttribute('data-custom-dropdown-open-value', false)\n }\n\n closeByEscape() {\n if (this.isInvisible(this.destinationMenuTarget)) return\n\n this.element.setAttribute('data-custom-dropdown-open-value', false)\n this.dispatch('focus', {\n target: this.element,\n })\n }\n\n coordinateDestinationToSource({detail}) {\n if (this.coordinatingFromSourceToDestination) return\n\n if(detail.technology === 'webchat') {\n return this.compose.focus()\n }\n\n if (detail.identifier === 'default') {\n return this.coordinateDestinationToDefaultSource()\n }\n\n const window = this.windowsValue.find(window => window.channel_id === detail.channelId.split(':')[1])\n\n this.coordinatingFromDestinationToSource = true\n\n if (window && window.open) {\n const destinationOption = this.destinationOptionTargets.find(option => option.dataset.identity === window.destination_id)\n destinationOption.click()\n } else {\n const destinationOption = this.destinationOptionTargets.find(option => option.dataset.technology === detail.technology)\n || this.destinationOptionTargets.find(option => detail.technology === 'whatsapp' && ['sms', 'whatsapp'].includes(option.dataset.technology))\n\n destinationOption?.click()\n }\n\n this.nextTick(() => this.coordinatingFromDestinationToSource = false, 100)\n\n this.syncCompose({\n window,\n technology: window?.technology || this.technologiesValue.find(t => t.name === detail.technology),\n })\n\n this.activeWindowValue = window\n }\n\n coordinateDestinationToDefaultSource() {\n const openWindow = this.openWindow\n this.coordinatingFromDestinationToSource = true\n\n if (openWindow) {\n const destinationOption = this.destinationOptionTargets.find(option => option.dataset.technology === openWindow.technology.name)\n destinationOption.click()\n } else {\n const destinationOption = this.destinationOptionTargets.find(option => option.dataset.technology === 'sms')\n destinationOption?.click()\n }\n\n this.nextTick(() => this.coordinatingFromDestinationToSource = false, 100)\n\n this.syncCompose({\n window: openWindow,\n technology: openWindow?.technology || this.technologiesValue.find(t => t.name === 'sms'),\n })\n\n this.activeWindowValue = openWindow\n }\n\n coordinateSourceToDestination({currentTarget}) {\n if (this.coordinatingFromDestinationToSource) return\n\n const {technology} = currentTarget.dataset\n const window = this.windowsValue.find(window => window.technology.name === technology && window.destination_id === currentTarget.dataset.identity)\n\n this.coordinatingFromSourceToDestination = true\n\n const identifier = window && window.open ? `${window.technology.name}:${window.channel_id}` : technology\n\n const sourceOption = this.sourceOptionTargets.find(option => option.dataset.optionValue === identifier)\n || this.sourceOptionTargets.find(option => option.dataset.identifier === technology)\n || this.sourceOptionTargets.find(option => option.dataset.identifier === 'default')\n\n // Messages sent to SMS technology of a WhatsApp identity will be nil\n sourceOption.click()\n\n this.nextTick(() => this.coordinatingFromSourceToDestination = false, 100)\n\n this.syncCompose({\n window,\n technology: window?.technology || this.technologiesValue.find(t => t.name === technology),\n })\n\n if (technology === 'mercadolibre') {\n this.displayMercadolibreNotice()\n } else {\n this.hideMercadolibreNotice()\n }\n\n this.element.setAttribute('data-custom-dropdown-open-value', false)\n this.activeWindowValue = window\n }\n\n syncCompose({window, technology}) {\n console.log(\n this.dispatch('technology:change', {\n target: this.element,\n detail: {\n window,\n technology,\n }\n })\n )\n\n this.dispatch('technology:change', {\n target: this.templatesMenuTarget,\n detail: {\n window,\n technology\n }\n })\n }\n\n displayMercadolibreNotice() {\n document.querySelector(\"mercadolibre-notice\").classList.remove(\"hidden\")\n }\n\n hideMercadolibreNotice() {\n if (document.querySelector(\"mercadolibre-notice\")) {\n document.querySelector(\"mercadolibre-notice\").classList.add(\"hidden\")\n }\n }\n\n windowUpdateTargetConnected() {\n if (this.compose.hasContent) {\n return this.windowUpdateTarget.remove()\n }\n\n const update = JSON.parse(this.windowUpdateTarget.dataset.payload)\n\n this.windowsValue = [\n ...this.windowsValue.filter(window => window.id !== update.id),\n update,\n ]\n\n this.syncCompose({\n window: update,\n technology: update.technology,\n })\n\n if (this.activeWindowValue?.id === update.id) {\n return this.windowUpdateTarget.remove()\n }\n\n this.activeWindowValue = update\n\n this.destinationOptionTargets\n .find(({dataset}) => dataset.identity === update.destination_id && dataset.technology === update.technology.name)\n .click()\n\n this.windowUpdateTarget.remove()\n }\n\n refreshedDestinationMenuTargetConnected() {\n const activeOption = Array\n .from(this.destinationMenuTarget.querySelectorAll('[data-compose--coordinator-target=\"destinationOption\"]'))\n .find(option => option.hasAttribute('data-active'))\n\n const optionInNewMenu = Array\n .from(this.refreshedDestinationMenuTarget.querySelectorAll('[data-compose--coordinator-target=\"destinationOption\"]'))\n .find(option => option.dataset.optionValue === activeOption?.dataset?.optionValue)\n\n if (optionInNewMenu && !optionInNewMenu.hasAttribute('data-active')) {\n this.coordinatingFromSourceToDestination = true\n optionInNewMenu.click()\n\n this.nextTick(() => this.coordinatingFromSourceToDestination = false, 100)\n }\n\n this.destinationMenuTarget.replaceWith(this.refreshedDestinationMenuTarget)\n this.refreshedDestinationMenuTarget.setAttribute('data-compose--coordinator-target', 'destinationMenu')\n\n this.nextTick(() => {\n if(this.destinationMenuTarget.hasAttribute('data-multiple')) {\n if(!optionInNewMenu) {\n this.destinationOptionTargets[0].click()\n }\n\n return this.show(this.destinationMenuTriggerTarget)\n }\n\n if (this.destinationOptionTargets.length === 1) {\n this.hide(this.destinationMenuTriggerTarget)\n this.element.setAttribute('data-custom-dropdown-open-value', false)\n } else {\n this.hide(this.inputContainerTarget)\n this.disableNoticeTarget.firstElementChild.textContent = this.translations.compose.unreachable\n this.show(this.disabledNoticeTarget)\n }\n })\n }\n\n // private\n\n get compose() {\n return this.application.getControllerForElementAndIdentifier(this.element, 'input')\n }\n\n get openWindow() {\n return this.findOpenWindowByTechnology('whatsapp')\n || this.findOpenWindowByTechnology('instagram')\n || this.findOpenWindowByTechnology('sms')\n || this.findOpenWindowByTechnology('mercadolibre')\n }\n\n findOpenWindowByTechnology(technology) {\n return this.windowsValue.find(window => window.technology.name === technology && window.open)\n }\n}\n"],"names":["coordinator_controller","ApplicationController","target","detail","window","option","destinationOption","t","openWindow","currentTarget","technology","identifier","update","_a","dataset","activeOption","optionInNewMenu","__publicField"],"mappings":"uNAEe,MAAKA,UAASC,CAAsB,CAmBjD,eAAe,CAAC,OAAAC,CAAM,EAAG,CACnB,KAAK,YAAY,KAAK,qBAAqB,GAAK,KAAK,sBAAsB,SAASA,CAAM,GAAK,KAAK,6BAA6B,SAASA,CAAM,GACpJ,KAAK,QAAQ,aAAa,kCAAmC,EAAK,CACtE,CAEE,eAAgB,CACV,KAAK,YAAY,KAAK,qBAAqB,IAE/C,KAAK,QAAQ,aAAa,kCAAmC,EAAK,EAClE,KAAK,SAAS,QAAS,CACrB,OAAQ,KAAK,OACd,CAAA,EACL,CAEE,8BAA8B,CAAC,OAAAC,CAAM,EAAG,CACtC,GAAI,KAAK,oCAAqC,OAE9C,GAAGA,EAAO,aAAe,UACvB,OAAO,KAAK,QAAQ,MAAK,EAG3B,GAAIA,EAAO,aAAe,UACxB,OAAO,KAAK,qCAAoC,EAGlD,MAAMC,EAAS,KAAK,aAAa,KAAKA,GAAUA,EAAO,aAAeD,EAAO,UAAU,MAAM,GAAG,EAAE,CAAC,CAAC,EAIpG,GAFA,KAAK,oCAAsC,GAEvCC,GAAUA,EAAO,KACO,KAAK,yBAAyB,KAAKC,GAAUA,EAAO,QAAQ,WAAaD,EAAO,cAAc,EACtG,MAAK,MAClB,CACL,MAAME,EAAoB,KAAK,yBAAyB,KAAKD,GAAUA,EAAO,QAAQ,aAAeF,EAAO,UAAU,GACjH,KAAK,yBAAyB,KAAKE,GAAUF,EAAO,aAAe,YAAc,CAAC,MAAO,UAAU,EAAE,SAASE,EAAO,QAAQ,UAAU,CAAC,EAE7IC,GAAA,MAA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