{"version":3,"file":"checkoutHelperService-B1qBlOuX.js","sources":["../../../../Coloplast.UI/src/multisite/services/checkoutHelperService.js"],"sourcesContent":["/**\r\n * Format a fieldName as it should be used in tracking\r\n * @param fieldName {string}\r\n * @return {string}\r\n */\r\nexport function trackingFieldName(fieldName) {\r\n\tif (!fieldName) return undefined;\r\n\treturn fieldName\r\n\t\t.split(/(?=[A-Z])/)\r\n\t\t.map((a) => a.charAt(0).toUpperCase() + a.substr(1))\r\n\t\t.join(\" \");\r\n}\r\n/**\r\n * Validate a field value against the validation rules\r\n * @param fieldValue {string|boolean|number|undefined}\r\n * @param options {{maxLength?: number, validatePattern?: string | RegExp, required?: boolean, apiValidationError?: string, allowPatterns?: Array, disallowPatterns?: Array}}\r\n * @return {{formatError: boolean, isValid: boolean, maxLengthError: boolean, requiredError: boolean, apiValidationError: string}}\r\n */\r\nexport function validateFieldState(\r\n\tfieldValue,\r\n\t{ maxLength, validatePattern, required, apiValidationError, allowPatterns, disallowPatterns } = {}\r\n) {\r\n\t// trim if the value is string to prevent submitting spaces\r\n\tconst value = typeof fieldValue === \"string\" ? fieldValue.trim() : fieldValue;\r\n\r\n\tconst requiredError = required ? !value : false;\r\n\tconst maxLengthError = maxLength && value && typeof value !== 'boolean' ? value?.length > maxLength : false;\r\n\tconst hasValidatePattern =\r\n\t\ttypeof validatePattern !== \"undefined\" &&\r\n\t\tvalidatePattern !== null &&\r\n\t\tvalidatePattern !== \"\";\r\n\r\n\tallowPatterns = allowPatterns || [];\r\n\tdisallowPatterns = disallowPatterns || [];\r\n\t// check if both the allowPatterns and disallowPatterns arrays empty and\r\n\t// if we have a normal validatePattern\r\n\tconst hasAllowPatterns = allowPatterns.length > 0;\r\n\tconst hasDisallowPatterns = disallowPatterns.length > 0;\r\n\tif (!hasAllowPatterns && !hasDisallowPatterns && hasValidatePattern) {\r\n\t\t// in this case we will push the simple regex to the allowPatterns array\r\n\t\tallowPatterns = [{\r\n\t\t\tvalidationRegExpr: validatePattern,\r\n\t\t}];\r\n\t}\r\n\tconst performValidation = hasAllowPatterns || hasDisallowPatterns || hasValidatePattern;\r\n\r\n\tconst validateDisallowPatternsResult = validateDisallowPatterns(String(value).toLowerCase(), disallowPatterns);\r\n\r\n\tconst formatError = performValidation && value\r\n\t\t? !(\r\n\t\t\tvalidateAllowPatterns(String(value).toLowerCase(), allowPatterns) &&\r\n\t\t\tvalidateDisallowPatternsResult.result\r\n\t\t)\r\n\t\t: false;\r\n\r\n\r\n\treturn {\r\n\t\trequiredError,\r\n\t\tmaxLengthError,\r\n\t\tformatError,\r\n\t\tformatErrorMessage: validateDisallowPatternsResult.validationErrorMessage || '',\r\n\t\tapiValidationError,\r\n\t\tisValid: !(\r\n\t\t\trequiredError ||\r\n\t\t\tmaxLengthError ||\r\n\t\t\tformatError ||\r\n\t\t\t!!apiValidationError\r\n\t\t),\r\n\t};\r\n}\r\n\r\n/**\r\n * Formats the API error messages into a structured format.\r\n * @param {string} messages - A string containing error messages separated by line breaks.\r\n * @returns {Array<{name: string, fieldName: string, message: string}>} An array of objects with name, fieldName, and message properties.\r\n * @example formatApiMessages(\"[DeliveryInformation.BillingAddress.Title, Invalid selection ]\")\r\n */\r\nexport function formatApiMessages(messages) {\r\n\tif (!messages) return [];\r\n\r\n\tconst lines = messages.split(\"\\r\\n\");\r\n\r\n\treturn lines.map((line, index) => {\r\n\t\tconst [name, message] = line\r\n\t\t\t.replace(\"[\", \"\")\r\n\t\t\t.replace(\"]\", \"\")\r\n\t\t\t.split(\",\");\r\n\r\n\t\tconst field = document.querySelector(\r\n\t\t\t`input[name=\"${name}\"], select[name=\"${name}\"]`\r\n\t\t);\r\n\t\tconst fieldName = field?.dataset.fieldName;\r\n\r\n\t\tif (fieldName && index === 0) {\r\n\t\t\t// Ensure focus is set to the field. If we want to to make this function more clean, we could move these sideeffects out of the code\r\n\t\t\tfocusOnField(fieldName);\r\n\t\t}\r\n\r\n\t\t// Ensure we always return a \"truthy\" string, so our validation correctly detects it.\r\n\t\treturn { name, fieldName, message: typeof message === 'string' && message !== \"\" ? message.trim() : \" \" };\r\n\t});\r\n}\r\n\r\n/**\r\n * Set focus to the first invalid field\r\n * @param fieldName {string}\r\n */\r\nexport function focusOnField(fieldName) {\r\n\tconst firstField = document.querySelector(\r\n\t\t`input[data-field-name=\"${fieldName}\"], select[data-field-name=\"${fieldName}\"]`\r\n\t);\r\n\tif (fieldName && firstField) {\r\n\t\tfirstField.focus();\r\n\t\tif (typeof window.scrollIntoView !== \"undefined\") {\r\n\t\t\twindow.scrollIntoView(firstField);\r\n\t\t}\r\n\t}\r\n}\r\n\r\n/**\r\n * Validate the value against an array of allowed patterns.\r\n * If any pattern matches return true. Otherwise false\r\n * @param {String} value\r\n * @param {Array[String]} allowPatterns\r\n */\r\nfunction validateAllowPatterns(value, allowPatterns) {\r\n\tif (allowPatterns.length == 0) {\r\n\t\treturn true;\r\n\t}\r\n\tfor (let allowPattern of allowPatterns) {\r\n\t\tconst pattern = typeof allowPattern.validationRegExpr == 'string' ? new RegExp(allowPattern.validationRegExpr, 'i') : allowPattern.validationRegExpr;\r\n\t\tif (pattern.test(value)) {\r\n\t\t\treturn true;\r\n\t\t}\r\n\t}\r\n\treturn false;\r\n}\r\n\r\n/**\r\n * Validate against an array of disallowed patterns.\r\n * If any pattern matches return false - otherwise return true\r\n * @param {*} value\r\n * @param {*} disallowPatterns\r\n */\r\nfunction validateDisallowPatterns(value, disallowPatterns) {\r\n\tfor (let disallowPattern of disallowPatterns) {\r\n\t\tconst pattern = new RegExp(disallowPattern.validationRegExpr, 'i');\r\n\t\tif (pattern.test(value)) {\r\n\t\t\treturn {\r\n\t\t\t\tresult: false,\r\n\t\t\t\tvalidationErrorMessage: disallowPattern.validationErrorMessage,\r\n\t\t\t};\r\n\t\t}\r\n\t}\r\n\treturn {\r\n\t\tresult: true,\r\n\t};\r\n}\r\n"],"names":["trackingFieldName","fieldName","a","validateFieldState","fieldValue","maxLength","validatePattern","required","apiValidationError","allowPatterns","disallowPatterns","value","requiredError","maxLengthError","hasValidatePattern","hasAllowPatterns","hasDisallowPatterns","performValidation","validateDisallowPatternsResult","validateDisallowPatterns","formatError","validateAllowPatterns","formatApiMessages","messages","line","index","name","message","field","focusOnField","firstField","allowPattern","disallowPattern"],"mappings":"AAKO,SAASA,EAAkBC,EAAW,CAC5C,GAAKA,EACL,OAAOA,EACL,MAAM,WAAW,EACjB,IAAKC,GAAMA,EAAE,OAAO,CAAC,EAAE,cAAgBA,EAAE,OAAO,CAAC,CAAC,EAClD,KAAK,GAAG,CACX,CAOO,SAASC,EACfC,EACA,CAAE,UAAAC,EAAW,gBAAAC,EAAiB,SAAAC,EAAU,mBAAAC,EAAoB,cAAAC,EAAe,iBAAAC,CAAgB,EAAK,CAAE,EACjG,CAED,MAAMC,EAAQ,OAAOP,GAAe,SAAWA,EAAW,KAAM,EAAGA,EAE7DQ,EAAgBL,EAAW,CAACI,EAAQ,GACpCE,EAAiBR,GAAaM,GAAS,OAAOA,GAAU,WAAYA,GAAA,YAAAA,EAAO,QAASN,EAAY,GAChGS,EACL,OAAOR,EAAoB,KAC3BA,IAAoB,MACpBA,IAAoB,GAErBG,EAAgBA,GAAiB,GACjCC,EAAmBA,GAAoB,GAGvC,MAAMK,EAAmBN,EAAc,OAAS,EAC1CO,EAAsBN,EAAiB,OAAS,EAClD,CAACK,GAAoB,CAACC,GAAuBF,IAEhDL,EAAgB,CAAC,CAChB,kBAAmBH,CACtB,CAAG,GAEF,MAAMW,EAAoBF,GAAoBC,GAAuBF,EAE/DI,EAAiCC,EAAyB,OAAOR,CAAK,EAAE,YAAW,EAAID,CAAgB,EAEvGU,EAAcH,GAAqBN,EACtC,EACDU,EAAsB,OAAOV,CAAK,EAAE,YAAW,EAAIF,CAAa,GAChES,EAA+B,QAE9B,GAGH,MAAO,CACN,cAAAN,EACA,eAAAC,EACA,YAAAO,EACA,mBAAoBF,EAA+B,wBAA0B,GAC7E,mBAAAV,EACA,QAAS,EACRI,GACAC,GACAO,GACEZ,EAE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