import*as Common from"../common/common.js";import*as TextUtils from"../text_utils/text_utils.js";export class CSSShadowModel{constructor(t){this._isBoxShadow=t,this._inset=!1,this._offsetX=CSSLength.zero(),this._offsetY=CSSLength.zero(),this._blurRadius=CSSLength.zero(),this._spreadRadius=CSSLength.zero(),this._color=Common.Color.Color.parse("black"),this._format=[_Part.OffsetX,_Part.OffsetY],this._important=!1}static parseTextShadow(t){return CSSShadowModel._parseShadow(t,!1)}static parseBoxShadow(t){return CSSShadowModel._parseShadow(t,!0)}static _parseShadow(t,s){const e=[],r=TextUtils.TextUtils.Utils.splitStringByRegexes(t,[Common.Color.Regex,/,/g]);let o=0;for(let n=0;n<r.length;n++)if(1===r[n].regexIndex){const s=r[n];e.push(t.substring(o,s.position)),o=s.position+1}e.push(t.substring(o,t.length));const a=[];for(let n=0;n<e.length;n++){const t=new CSSShadowModel(s);t._format=[];let r=!0;const o=[/!important/gi,/inset/gi,Common.Color.Regex,CSSLength.Regex],f=TextUtils.TextUtils.Utils.splitStringByRegexes(e[n],o);for(let s=0;s<f.length;s++){const e=f[s];if(-1===e.regexIndex){if(/\S/.test(e.value))return[];r=!0}else{if(!r)return[];if(r=!1,0===e.regexIndex)t._important=!0,t._format.push(_Part.Important);else if(1===e.regexIndex)t._inset=!0,t._format.push(_Part.Inset);else if(2===e.regexIndex){const s=Common.Color.Color.parse(e.value);if(!s)return[];t._color=s,t._format.push(_Part.Color)}else if(3===e.regexIndex){const s=CSSLength.parse(e.value);if(!s)return[];const r=t._format.length>0?t._format[t._format.length-1]:"";r===_Part.OffsetX?(t._offsetY=s,t._format.push(_Part.OffsetY)):r===_Part.OffsetY?(t._blurRadius=s,t._format.push(_Part.BlurRadius)):r===_Part.BlurRadius?(t._spreadRadius=s,t._format.push(_Part.SpreadRadius)):(t._offsetX=s,t._format.push(_Part.OffsetX))}}}if(i(t,_Part.OffsetX,1,1)||i(t,_Part.OffsetY,1,1)||i(t,_Part.Color,0,1)||i(t,_Part.BlurRadius,0,1)||i(t,_Part.Inset,0,s?1:0)||i(t,_Part.SpreadRadius,0,s?1:0)||i(t,_Part.Important,0,1))return[];a.push(t)}return a;function i(t,s,e,r){let o=0;for(let a=0;a<t._format.length;a++)t._format[a]===s&&o++;return o<e||o>r}}setInset(t){this._inset=t,-1===this._format.indexOf(_Part.Inset)&&this._format.unshift(_Part.Inset)}setOffsetX(t){this._offsetX=t}setOffsetY(t){this._offsetY=t}setBlurRadius(t){if(this._blurRadius=t,-1===this._format.indexOf(_Part.BlurRadius)){const t=this._format.indexOf(_Part.OffsetY);this._format.splice(t+1,0,_Part.BlurRadius)}}setSpreadRadius(t){if(this._spreadRadius=t,-1===this._format.indexOf(_Part.SpreadRadius)){this.setBlurRadius(this._blurRadius);const t=this._format.indexOf(_Part.BlurRadius);this._format.splice(t+1,0,_Part.SpreadRadius)}}setColor(t){this._color=t,-1===this._format.indexOf(_Part.Color)&&this._format.push(_Part.Color)}isBoxShadow(){return this._isBoxShadow}inset(){return this._inset}offsetX(){return this._offsetX}offsetY(){return this._offsetY}blurRadius(){return this._blurRadius}spreadRadius(){return this._spreadRadius}color(){return this._color}asCSSText(){const t=[];for(let s=0;s<this._format.length;s++){const e=this._format[s];e===_Part.Inset&&this._inset?t.push("inset"):e===_Part.OffsetX?t.push(this._offsetX.asCSSText()):e===_Part.OffsetY?t.push(this._offsetY.asCSSText()):e===_Part.BlurRadius?t.push(this._blurRadius.asCSSText()):e===_Part.SpreadRadius?t.push(this._spreadRadius.asCSSText()):e===_Part.Color?t.push(this._color.asString(this._color.format())):e===_Part.Important&&this._important&&t.push("!important")}return t.join(" ")}}export const _Part={Inset:"I",OffsetX:"X",OffsetY:"Y",BlurRadius:"B",SpreadRadius:"S",Color:"C",Important:"M"};export class CSSLength{constructor(t,s){this.amount=t,this.unit=s}static parse(t){const s=new RegExp("^(?:"+CSSLength.Regex.source+")$","i"),e=t.match(s);return e?e.length>2&&e[2]?new CSSLength(parseFloat(e[1]),e[2]):CSSLength.zero():null}static zero(){return new CSSLength(0,"")}asCSSText(){return this.amount+this.unit}}CSSLength.Regex=new RegExp("([+-]?(?:[0-9]*[.])?[0-9]+(?:[eE][+-]?[0-9]+)?)(ch|cm|em|ex|in|mm|pc|pt|px|rem|vh|vmax|vmin|vw)|[+-]?(?:0*[.])?0+(?:[eE][+-]?[0-9]+)?","gi");