import e from"@babel/runtime/helpers/toConsumableArray";import t from"@babel/runtime/helpers/classCallCheck";import r from"@babel/runtime/helpers/createClass";import n from"@babel/runtime/helpers/defineProperty";import{map as i,snakeCase as a,find as o,some as s,reject as u}from"lodash";import{NAME_IS_MISSING_MESSAGE as c,ALREADY_EXISTS_MESSAGE as d,GRADIENT_NEW_NAME as l}from"./configuration/constants.js";function h(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function f(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?h(Object(r),!0).forEach((function(t){n(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):h(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}var g=function(){function h(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[],i=arguments.length>2?arguments[2]:void 0;t(this,h),n(this,"gradients",void 0),n(this,"defaultGradients",void 0),n(this,"onGradientsChange",void 0),this.gradients=e,this.defaultGradients=r,this.onGradientsChange=i}return r(h,[{key:"generateId",value:function(t){for(var r=new Set(i([].concat(e(this.gradients),e(this.defaultGradients)),"id")),n=a(t),o=0;r.has(n+(o?"_".concat(o):""));)o+=1;return n+(o?"_".concat(o):"")}},{key:"getById",value:function(e){return o(this.gradients,{id:e})||o(this.defaultGradients,{id:e})}},{key:"getByName",value:function(t){var r=(t||"").toLocaleLowerCase();return o([].concat(e(this.gradients),e(this.defaultGradients)),(function(e){return e.name.toLocaleLowerCase()===r}))}},{key:"newName",value:function(){for(var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:l,t=0;this.getByName(e+(t?" ".concat(t):""));)t+=1;return e+(t?" ".concat(t):"")}},{key:"isDefault",value:function(e){return s(this.defaultGradients,{id:e})}},{key:"getHelperText",value:function(e){var t=e.name,r=e.allowNoname,n=e.gradientId;if(!r&&!t)return{error:!1,helperText:c};var i=this.getByName(t);return i&&i.id!==n?{error:!0,helperText:d}:null}},{key:"add",value:function(t,r){if(!t)throw new Error(c);if(this.gradients.map((function(e){return e.name.toLocaleLowerCase()})).includes(t.toLocaleLowerCase()))throw new Error(d);var n={id:this.generateId(t),name:t,gradientStops:r};return this.onGradientsChange([].concat(e(this.gradients),[n])),n}},{key:"remove",value:function(e){var t=u(this.gradients,{id:e});return this.onGradientsChange(t),t}},{key:"update",value:function(e,t){var r=this.gradients.map((function(r){return r.id===e?f(f({},r),t):r}));return this.onGradientsChange(r),r}}]),h}();export{g as GradientList};
//# sourceMappingURL=GradientList.js.map
