Liferay.Loader.define("data-engine-js-components-web@1.0.107/js/core/reducers/fieldReducer.es",["module","exports","require","../../utils/fieldSupport","../../utils/repeatable.es","../../utils/visitors.es","../actions/eventTypes.es"],(function(e,t,r){window;{Object.defineProperty(t,"__esModule",{value:!0}),t.createRepeatedField=createRepeatedField,t.updateNestedFieldNames=updateNestedFieldNames,t.default=function fieldReducer(e,t){switch(t.type){case s.EVENT_TYPES.FIELD.BLUR:{const{fieldInstance:r}=t.payload;return{pages:new d.PagesVisitor(e.pages).mapFields((e=>{const t=e.name===r.name&&e.required&&""===r.value;return _objectSpread(_objectSpread({},e),{},{displayErrors:!!e.displayErrors||t,focused:!t&&e.focused})}))}}case s.EVENT_TYPES.FIELD.FOCUS:{const{fieldInstance:r}=t.payload;return{pages:new d.PagesVisitor(e.pages).mapFields((e=>{const t=e.name===r.name;return _objectSpread(_objectSpread({},e),{},{focused:t})}))}}case s.EVENT_TYPES.FIELD.REMOVED:{const r=new d.PagesVisitor(e.pages),getParsedName=(e,t)=>t?(0,n.parseNestedFieldName)(e):(0,n.parseName)(e);return{pages:r.mapColumns((e=>{const filter=(e,r)=>{const a=e.filter((({name:e})=>e!==t.payload)),d=getParsedName(t.payload,r),s=a.filter((({fieldName:e})=>e===d.fieldName)).map(((e,t)=>{const r=(0,n.generateName)(e.name,{repeatedIndex:t});return _objectSpread(_objectSpread({},e),{},{nestedFields:updateNestedFieldNames(r,e.nestedFields),newName:r})}));return a.map((e=>{const t=s.find((({name:t})=>t===e.name));return t&&(e=_objectSpread(_objectSpread({},t),{},{name:t.newName})),_objectSpread(_objectSpread({},e),{},{nestedFields:e.nestedFields?filter(e.nestedFields,e.name):[]})}))};return _objectSpread(_objectSpread({},e),{},{fields:filter(e.fields)})}))}}case s.EVENT_TYPES.FIELD.REPEATED:return{pages:new d.PagesVisitor(e.pages).mapColumns((e=>{const addRepeatedField=e=>{const r=e.reduce(((e=-1,r,a)=>(r.name===t.payload&&(e=a),e)),-1);if(r>-1){const t=r+1,a=createRepeatedField(e[r],t);let d=0;return[...e.slice(0,t),a,...e.slice(t)].map((e=>{if(e.fieldName===a.fieldName){const t=(0,n.generateName)(e.name,{repeatedIndex:d++});return _objectSpread(_objectSpread(_objectSpread({},e),e.editorConfig&&{editorConfig:updateEditorConfigFieldName(e.editorConfig,t)}),{},{name:t,nestedFields:updateNestedFieldNames(t,e.nestedFields)})}return e}))}return e.map((e=>_objectSpread(_objectSpread({},e),{},{nestedFields:e.nestedFields?addRepeatedField(e.nestedFields):[]})))};return _objectSpread(_objectSpread({},e),{},{fields:addRepeatedField(e.fields)})}))};default:return e}};var a=r("../../utils/fieldSupport"),n=r("../../utils/repeatable.es"),d=r("../../utils/visitors.es"),s=r("../actions/eventTypes.es");function ownKeys(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);t&&(a=a.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,a)}return r}function _objectSpread(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?ownKeys(Object(r),!0).forEach((function(t){_defineProperty(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):ownKeys(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}function _defineProperty(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function createRepeatedField(e,t){const r=(0,a.generateInstanceId)(),{locale:d,name:s,nestedFields:o,predefinedValue:i}=e;let c;return e.localizedValue&&(c=Object.keys(e.localizedValue).reduce(((e,t)=>(e[t]="",e)),{}),c[d]=null!=i?i:c[d]),_objectSpread(_objectSpread({},e),{},{confirmationValue:"",instanceId:r,localizedValue:c,name:(0,n.generateName)(s,{instanceId:r,repeatedIndex:t}),nestedFields:null==o?void 0:o.map((e=>createRepeatedField(e))),valid:!0,value:i})}function updateNestedFieldNames(e,t){return(t||[]).map((t=>{const r=(0,n.generateNestedFieldName)(t.name,e);return _objectSpread(_objectSpread(_objectSpread({},t),t.editorConfig&&{editorConfig:updateEditorConfigFieldName(t.editorConfig,r)}),{},{name:r,nestedFields:updateNestedFieldNames(r,t.nestedFields)},(0,n.parseNestedFieldName)(r))}))}function updateEditorConfigFieldName(e,t){const r=_objectSpread({},e);for(const[e,a]of Object.entries(r))if("string"==typeof a){const d=(0,n.parseName)(decodeURIComponent(a));if(Object.keys(d).length){const s=encodeURIComponent((0,n.generateName)(null,d));r[e]=a.replace(s,encodeURIComponent(t)+"selectItem")}}return r}}}));
//# sourceMappingURL=fieldReducer.es.js.map