{"version":3,"file":"packages_controller-C3L8zP6M.js","sources":["../../../app/javascript/controllers/payments/packages_controller.js"],"sourcesContent":["import ApplicationController from '../application_controller'\n\nimport { useNumberHelpers } from '../mixins/useNumberHelpers'\nimport {MoneyFormatter} from \"@/controllers/models/money_formatter\";\n\nexport default class extends ApplicationController {\n static values = {\n agreement: String,\n activePackageId: String,\n creditBundle: String,\n swap: Boolean,\n smsCost: Number,\n info: Object,\n countryName: String,\n countryCurrency: Object,\n }\n\n static targets = [\n 'monthlyAgreementPrice',\n 'yearlyAgreementPrice',\n 'seat',\n 'packagePrice',\n 'monthlyConversations',\n 'monthlySms',\n 'package',\n 'creditsRange',\n 'seatRange',\n 'totalPrice',\n 'name',\n 'monthlySmsSlider',\n 'submitButton',\n ]\n\n initialize() {\n super.initialize()\n\n this.packageElement = this.packageTargets.find(element => element.dataset.id === this.activePackageIdValue)\n\n console.log(this.countryCurrencyValue.subunit_to_unit)\n this.moneyFormatter = new MoneyFormatter(\n this.countryCurrencyValue.code,\n {\n divide: true,\n divisor: this.countryCurrencyValue.subunit_to_unit\n }\n )\n\n this.initialAgreemet = this.agreementValue\n this.initialPackage = this.activePackageIdValue\n\n this.initialCredit = this.infoValue.features.credit.available\n this.initialCreditBundle = this.infoValue.features.credit_bundle.available\n\n this.initialSeat = this.infoValue.features.seat.available\n }\n\n connect() {\n useNumberHelpers(this)\n super.connect()\n }\n\n onAgreementChange({ detail: agreement }) {\n this.agreementValue = agreement\n\n this.packageTargets.forEach(target => {\n this.dispatch('agreement:change', {\n target,\n detail: agreement\n })\n })\n\n this.syncSubmitButton()\n this.syncPackagePrice()\n }\n\n onPackageChange({ detail: info, srcElement }) {\n this.infoValue = info\n\n const seat = parseInt(info.features.seat.available)\n this.seatTarget.innerText = seat\n\n this.packageElement = srcElement\n this.syncPackagePrice()\n\n const credit = parseInt(info.features.credit.available)\n\n this.monthlyConversationsTarget.innerText = t.payments.plans.index.wizard.aside.monthly_conversations.description.replace(\n '%{count}',\n this.numberWithDelimiter(credit)\n )\n\n this.monthlySmsTarget.innerText = t.payments.plans.index.wizard.aside.sms_in_country.description.replace(\n '%{count}',\n this.numberWithDelimiter(credit / this.smsCostValue)\n )\n\n this.dispatch('change', {\n target: this.seatRangeTarget,\n detail: seat,\n })\n\n this.dispatch('change', {\n target: this.creditsRangeTarget,\n detail: credit,\n })\n\n this.dispatch('change', {\n target: this.smsRangeTarget,\n detail: credit / this.smsCostValue,\n })\n\n this.nameTarget.innerText = srcElement.querySelector('[data-payments--package-target=\"name\"]').innerText\n this.syncSubmitButton()\n }\n\n syncPackagePrice() {\n let priceTarget;\n\n if(this.agreementValue === 'yearly') {\n priceTarget = this.packageElement.querySelector('[data-payments--package-target=\"yearlyAgreementPrice\"]')\n } else {\n priceTarget = this.packageElement.querySelector('[data-payments--package-target=\"monthlyAgreementPrice\"]')\n }\n\n this.packagePriceTarget.innerText = priceTarget.innerText\n this.totalPriceTarget.innerText = priceTarget.innerText\n }\n\n onFeatureLimitChange({ detail: quota, srcElement }) {\n if(srcElement === this.seatRangeTarget) {\n this.modifySeatAndRecalculateTotal(quota)\n } else {\n this.modifyCreditBundleAndRecalculateTotal(quota)\n }\n\n this.syncSubmitButton()\n }\n\n modifySeatAndRecalculateTotal(quota) {\n this.infoValue = {\n ...this.infoValue,\n features: {\n ...this.infoValue.features,\n seat: {\n ...this.infoValue.features.seat,\n available: quota\n }\n }\n }\n\n this.calculateTotal()\n }\n\n modifyCreditBundleAndRecalculateTotal(quota) {\n this.infoValue = {\n ...this.infoValue,\n features: {\n ...this.infoValue.features,\n credit_bundle: {\n ...this.infoValue.features.credit_bundle,\n available: quota === this.infoValue.features.credit.available ? 0 : quota\n }\n }\n }\n\n this.monthlySmsSliderTarget.innerText = t.static.pricing_v2.channels.sms\n .replace('%{count}', this.numberWithDelimiter(Math.floor(quota / this.smsCostValue)))\n .replace('%{country}', this.countryNameValue)\n\n this.monthlyConversationsTarget.innerText = t.payments.plans.index.wizard.aside.monthly_conversations.description.replace(\n '%{count}',\n this.numberWithDelimiter(quota)\n )\n\n this.monthlySmsTarget.innerText = t.payments.plans.index.wizard.aside.sms_in_country.description.replace(\n '%{count}',\n this.numberWithDelimiter(Math.floor(quota / this.smsCostValue))\n )\n\n this.calculateTotal()\n }\n\n calculateTotal() {\n if(this.agreementValue === 'yearly') {\n const yearlyTotalValue = this.getTotalValueForAgreement('yearly')\n\n this.totalPriceTarget.innerText = t.payments.plans.index.wizard.aside.total.price.replace(\n '%{price}',\n this.moneyFormatter.humanizeWithSymbol(yearlyTotalValue * this.countryCurrencyValue.multiplier)\n )\n } else {\n const monthlyTotalValue = this.getTotalValueForAgreement('monthly')\n\n this.totalPriceTarget.innerText = t.payments.plans.index.wizard.aside.total.price.replace(\n '%{price}',\n this.moneyFormatter.humanizeWithSymbol(monthlyTotalValue * this.countryCurrencyValue.multiplier)\n )\n }\n }\n\n syncSubmitButton() {\n this.submitButtonTarget.disabled = !this.packageChanged\n }\n\n getTotalValueForAgreement(agreementType) {\n return Object.entries(this.infoValue.features).map(([key, feature]) => {\n if(key === 'credit_bundle') {\n const subunits = agreementType === 'monthly' ? feature.monthly_base_price : feature.yearly_base_price\n return subunits * feature.available\n }\n\n return ((agreementType === 'monthly' ? feature.monthly_base_price : feature.yearly_base_price) || 0) * feature.available\n }).reduce((a, b) => a + b, 0)\n }\n\n get packageChanged() {\n return this.packageElement.dataset.id !== this.initialPackage\n || this.agreementValue !== this.initialAgreemet\n || this.infoValue.features.seat.available !== this.initialSeat\n || this.infoValue.features.credit.available !== this.initialCredit\n || this.infoValue.features.credit_bundle.available !== this.initialCreditBundle\n }\n}\n"],"names":["packages_controller","ApplicationController","element","MoneyFormatter","useNumberHelpers","agreement","target","info","srcElement","seat","credit","priceTarget","quota","yearlyTotalValue","monthlyTotalValue","agreementType","key","feature","b","__publicField"],"mappings":"qOAKe,MAAKA,UAASC,CAAsB,CA4BjD,YAAa,CACX,MAAM,WAAU,EAEhB,KAAK,eAAiB,KAAK,eAAe,KAAKC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