// All material copyright ESRI, All Rights Reserved, unless otherwise specified.
// See http://@sbaseurl@/jsapi/jsapi/esri/copyright.txt and http://www.arcgis.com/apps/webappbuilder/copyright.txt for details.

//>>built
define(["dojo/_base/lang","dojo/_base/array","jimu/utils","./presetUtils"],function(m,n,q,p){var d={createPresetGroups:function(b,c){d.config=b;d._jimuLayerInfos=c;if(b.editor.presetInfos&&0<Object.keys(b.editor.presetInfos).length&&(b.attributeActionGroups?b.attributeActionGroups&&!b.attributeActionGroups.hasOwnProperty("Preset")&&(b.attributeActionGroups.Preset={}):b.attributeActionGroups={Intersection:{},Address:{},Coordinates:{},Preset:{}},!(b.attributeActionGroups&&b.attributeActionGroups.Preset&&
0<Object.keys(b.attributeActionGroups.Preset).length)))for(var f in b.editor.presetInfos)d._addPresetGroupForField(b.editor.configInfos||b.editor.layerInfos,f)},_getDomainListForPresetGroup:function(b,c,f){var g=[];n.forEach(c.domain.codedValues,m.hitch(this,function(a){var e={showInList:!0,value:a.code,label:a.name,isDefault:!1};b.showOnlyDomainFields=!0;var h=d.config.editor.presetInfos[f][0];"esriFieldTypeInteger"===b.dataType&&(h=Number(h));0<d.config.editor.presetInfos[f].length?a.code===h&&
(e.isDefault=!0):0===g.length&&(e.isDefault=!0);g.push(e)}));return g},_addPresetGroupForField:function(b,c){var f;n.some(b,m.hitch(this,function(g){if(f)return!0;if(g.fieldValues&&g.fieldValues[c])for(var a=g.fieldValues[c],e=0;e<a.length;e++)if("Preset"===a[e].actionName&&a[e].enabled){a[e].attributeActionGroupName=c;var h=d._jimuLayerInfos.getLayerOrTableInfoById(g.featureLayer.id);if(h&&h.layerObject&&h.layerObject.fields){e=p.getFieldInfoByFieldName(h.layerObject.fields,c);h=q.getDefaultPortalFieldInfo(e).label+
" ("+c+")";a={};a.name=h;0<["esriFieldTypeInteger","esriFieldTypeSmallInteger","esriFieldTypeInteger","esriFieldTypeSingle","esriFieldTypeDouble"].indexOf(e.type)?a.dataType="esriFieldTypeInteger":a.dataType=e.type;a.showOnlyDomainFields=!1;a.hideInPresetDisplay=!1;a.appliedOn=d._getPresetFieldsForAppliedOn(e,a);if(e.domain&&"range"!==e.domain.type)a.presetValue=d._getDomainListForPresetGroup(a,e,c);else if("esriFieldTypeDate"===e.type){var k,l;0<d.config.editor.presetInfos[c].length?(isNaN(d.config.editor.presetInfos[c][0])||
""===d.config.editor.presetInfos[c][0]||(l=new Date(d.config.editor.presetInfos[c][0])),d.config.editor.presetInfos[c][1]&&!isNaN(d.config.editor.presetInfos[c][1])&&(k=new Date(d.config.editor.presetInfos[c][1])),k=l&&k?new Date(l.getFullYear(),l.getMonth(),l.getDate(),k.getHours(),k.getMinutes(),k.getSeconds(),k.getMilliseconds()):l,a.presetValue=k?{dateType:"fixed",dateTime:k.getTime()}:{dateType:"fixed",dateTime:""}):a.presetValue={dateType:"fixed",dateTime:""}}else a.presetValue=0<d.config.editor.presetInfos[c].length?
d.config.editor.presetInfos[c][0]:null;d.config.attributeActionGroups.Preset[a.name]=a;f=!0;break}}!f&&g.relationshipInfos&&0<g.relationshipInfos.length&&(f=d._addPresetGroupForField(g.relationshipInfos,c))}));return f},_isPresetEnabledForLayer:function(b,c,f,g){if(b.fieldValues[c.name])for(var a=b.fieldValues[c.name],e=0;e<a.length;e++)if("Preset"===a[e].actionName&&a[e].enabled){var h=d._jimuLayerInfos.getLayerOrTableInfoById(b.featureLayer.id).layerObject.fields;p.getFieldInfoByFieldName(h,c.name).type===
c.type&&(a[e].attributeActionGroupName=g.name,f.hasOwnProperty(b.featureLayer.id)?f[b.featureLayer.id].push(c.name):f[b.featureLayer.id]=[c.name])}return f},_getPresetFieldsForAppliedOn:function(b,c){var f={};n.forEach(d.config.editor.configInfos||d.config.editor.layerInfos,m.hitch(this,function(g){g.fieldValues&&g.fieldValues[b.name]&&(f=d._isPresetEnabledForLayer(g,b,f,c));g.relationshipInfos&&0<g.relationshipInfos.length&&n.forEach(g.relationshipInfos,m.hitch(this,function(a){a.fieldValues&&a.fieldValues[b.name]&&
(f=d._isPresetEnabledForLayer(a,b,f,c))}))}));return f}};return d});