import{__spreadArray as t,__assign as e}from'./../../ext-esm/tslib/tslib.es6.js';import{map as r,snakeCase as n,find as o,some as i,reject as a}from"lodash";import{NAME_IS_MISSING_MESSAGE as s,ALREADY_EXISTS_MESSAGE as d,GRADIENT_NEW_NAME as u}from"./configuration/constants.js";var h=function(){function h(t,e,r){void 0===t&&(t=[]),void 0===e&&(e=[]),this.gradients=t,this.defaultGradients=e,this.onGradientsChange=r}return h.prototype.generateId=function(e){for(var o=new Set(r(t(t([],this.gradients,!0),this.defaultGradients,!0),"id")),i=n(e),a=0;o.has(i+(a?"_".concat(a):""));)a+=1;return i+(a?"_".concat(a):"")},h.prototype.getById=function(t){return o(this.gradients,{id:t})||o(this.defaultGradients,{id:t})},h.prototype.getByName=function(e){var r=(e||"").toLocaleLowerCase();return o(t(t([],this.gradients,!0),this.defaultGradients,!0),(function(t){return t.name.toLocaleLowerCase()===r}))},h.prototype.newName=function(t){void 0===t&&(t=u);for(var e=0;this.getByName(t+(e?" ".concat(e):""));)e+=1;return t+(e?" ".concat(e):"")},h.prototype.isDefault=function(t){return i(this.defaultGradients,{id:t})},h.prototype.getHelperText=function(t){var e=t.name,r=t.allowNoname,n=t.gradientId;if(!r&&!e)return{error:!1,helperText:s};var o=this.getByName(e);return o&&o.id!==n?{error:!0,helperText:d}:null},h.prototype.add=function(e,r){if(!e)throw new Error(s);if(this.gradients.map((function(t){return t.name.toLocaleLowerCase()})).includes(e.toLocaleLowerCase()))throw new Error(d);var n={id:this.generateId(e),name:e,gradientStops:r};return this.onGradientsChange(t(t([],this.gradients,!0),[n],!1)),n},h.prototype.remove=function(t){var e=a(this.gradients,{id:t});return this.onGradientsChange(e),e},h.prototype.update=function(t,r){var n=this.gradients.map((function(n){return n.id===t?e(e({},n),r):n}));return this.onGradientsChange(n),n},h}();export{h as GradientList};
