define(["ko","mageUtils"],(function(ko,utils){"use strict";var optionsText,optionsValue,optionTitle,captionPlaceholder={},nbspRe=/&nbsp;/g;ko.bindingHandlers.optgroup={init:function(element){if("select"!==ko.utils.tagNameLower(element))throw new Error("options binding applies only to SELECT elements");for(;element.length>0;)element.remove(0)},update:function(element,valueAccessor,allBindings){var captionValue,filteredArray,previousSelectedValues,previousScrollTop=!(0===element.length)&&element.multiple?element.scrollTop:null,includeDestroyed=allBindings.get("optionsIncludeDestroyed"),arrayToDomNodeChildrenOptions={},unwrappedArray=ko.utils.unwrapObservable(valueAccessor()),itemUpdate=!1,callback=setSelectionCallback,nestedOptionsLevel=-1;function selectedOptions(){return ko.utils.arrayFilter(element.options,(function(node){return node.selected}))}function applyToObject(object,predicate,defaultValue){var predicateType=typeof predicate;return"function"===predicateType?predicate(object):"string"===predicateType?object[predicate]:defaultValue}function setSelectionCallback(newOptions){var isSelected;previousSelectedValues.length&&newOptions.value&&(isSelected=ko.utils.arrayIndexOf(previousSelectedValues,ko.selectExtensions.readValue(newOptions.value))>=0,ko.utils.setOptionNodeSelectionState(newOptions.value,isSelected),itemUpdate&&!isSelected&&ko.dependencyDetection.ignore(ko.utils.triggerEvent,null,[element,"change"]))}function strPad(string,times){return new Array(times+1).join(string)}optionsText=ko.utils.unwrapObservable(allBindings.get("optionsText"))||"text",optionsValue=ko.utils.unwrapObservable(allBindings.get("optionsValue"))||"value",optionTitle=optionsText+"title",previousSelectedValues=element.multiple?ko.utils.arrayMap(selectedOptions(),ko.selectExtensions.readValue):element.selectedIndex>=0?[ko.selectExtensions.readValue(element.options[element.selectedIndex])]:[],unwrappedArray&&(void 0===unwrappedArray.length&&(unwrappedArray=[unwrappedArray]),(filteredArray=ko.utils.arrayFilter(unwrappedArray,(function(item){return!(item&&!item.label)&&(includeDestroyed||null==item||!ko.utils.unwrapObservable(item._destroy))}))).map((function recursivePathBuilder(obj){obj[optionTitle]=(this&&this[optionTitle]?this[optionTitle]+"/":"")+obj[optionsText].trim(),Array.isArray(obj[optionsValue])&&obj[optionsValue].map(recursivePathBuilder,obj)}),null)),arrayToDomNodeChildrenOptions.beforeRemove=function(option){element.removeChild(option)},allBindings.has("optionsAfterRender")&&(callback=function(arrayEntry,newOptions){setSelectionCallback(arrayEntry),ko.dependencyDetection.ignore(allBindings.get("optionsAfterRender"),null,[newOptions[0],arrayEntry!==captionPlaceholder?arrayEntry:void 0])}),filteredArray=function formatOptions(options){var res=[];++nestedOptionsLevel||allBindings.has("optionsCaption")&&null!=(captionValue=ko.utils.unwrapObservable(allBindings.get("optionsCaption")))&&!1!==captionValue&&res.push(captionPlaceholder);return ko.utils.arrayForEach(options,(function(option){var value=applyToObject(option,optionsValue,option),label=applyToObject(option,optionsText,value)||"",disabled=applyToObject(option,"disabled",!1)||!1,obj={};obj[optionTitle]=applyToObject(option,optionsText+"title",value),disabled&&(obj.disabled=disabled),option.hasOwnProperty("__disableTmpl")&&(obj.__disableTmpl=option.__disableTmpl),label=label.replace(nbspRe,"").trim(),Array.isArray(value)?(obj[optionsText]=strPad("&nbsp;",4*nestedOptionsLevel)+label,res.push(obj),res=res.concat(formatOptions(value))):(obj[optionsText]=strPad("   ",2*nestedOptionsLevel)+label,obj[optionsValue]=value,res.push(obj))})),nestedOptionsLevel--,res}(filteredArray),ko.utils.setDomNodeChildrenFromArrayMapping(element,filteredArray,(function(arrayEntry,oldOptions){var option;oldOptions.length&&(previousSelectedValues=oldOptions[0].selected?[ko.selectExtensions.readValue(oldOptions[0])]:[],itemUpdate=!0);arrayEntry===captionPlaceholder?(option=element.ownerDocument.createElement("option"),ko.utils.setTextContent(option,allBindings.get("optionsCaption")),ko.selectExtensions.writeValue(option,void 0)):void 0===arrayEntry[optionsValue]?(option=arrayEntry.__disableTmpl?'<optgroup label="'+arrayEntry[optionsText]+'"></optgroup>':utils.template('<optgroup label="${ $.label }"></optgroup>',{label:arrayEntry[optionsText],title:arrayEntry[optionsText+"title"]}),option=ko.utils.parseHtmlFragment(option)[0]):((option=element.ownerDocument.createElement("option")).setAttribute("data-title",arrayEntry[optionsText+"title"]),ko.selectExtensions.writeValue(option,arrayEntry[optionsValue]),ko.utils.setTextContent(option,arrayEntry[optionsText]));return[option]}),arrayToDomNodeChildrenOptions,callback),ko.dependencyDetection.ignore((function(){allBindings.get("valueAllowUnset")&&allBindings.has("value")?ko.selectExtensions.writeValue(element,ko.utils.unwrapObservable(allBindings.get("value")),!0):(element.multiple?previousSelectedValues.length&&selectedOptions().length<previousSelectedValues.length:previousSelectedValues.length&&element.selectedIndex>=0?ko.selectExtensions.readValue(element.options[element.selectedIndex])!==previousSelectedValues[0]:previousSelectedValues.length||element.selectedIndex>=0)&&ko.utils.triggerEvent(element,"change")})),previousScrollTop&&Math.abs(previousScrollTop-element.scrollTop)>20&&(element.scrollTop=previousScrollTop)}}}));