import*as Common from"../common/common.js";import*as HostModule from"../host/host.js";import*as Platform from"../platform/platform.js";import*as TextEditor from"../text_editor/text_editor.js";import*as TextUtils from"../text_utils/text_utils.js";import{cssMetadata,GridAreaRowRegex}from"./CSSMetadata.js";export class CSSProperty{constructor(t,e,s,i,n,r,o,a,l,h){this.ownerStyle=t,this.index=e,this.name=s,this.value=i,this.important=n,this.disabled=r,this.parsedOk=o,this.implicit=a,this.text=l,this.range=h?TextUtils.TextRange.TextRange.fromObject(h):null,this._active=!0,this._nameRange=null,this._valueRange=null,this._invalidProperty=null}static parsePayload(t,e,s){return new CSSProperty(t,e,s.name,s.value,s.important||!1,s.disabled||!1,!("parsedOk"in s)||Boolean(s.parsedOk),Boolean(s.implicit),s.text,s.range)}_ensureRanges(){if(this._nameRange&&this._valueRange)return;const t=this.range,e=this.text?new TextUtils.Text.Text(this.text):null;if(!t||!e)return;const s=e.value().indexOf(this.name),i=e.value().lastIndexOf(this.value);if(-1===s||-1===i||s>i)return;const n=new TextUtils.TextRange.SourceRange(s,this.name.length),r=new TextUtils.TextRange.SourceRange(i,this.value.length);function o(t,e,s){return 0===t.startLine&&(t.startColumn+=s,t.endColumn+=s),t.startLine+=e,t.endLine+=e,t}this._nameRange=o(e.toTextRange(n),t.startLine,t.startColumn),this._valueRange=o(e.toTextRange(r),t.startLine,t.startColumn)}nameRange(){return this._ensureRanges(),this._nameRange}valueRange(){return this._ensureRanges(),this._valueRange}rebase(t){this.ownerStyle.styleSheetId===t.styleSheetId&&this.range&&(this.range=this.range.rebaseAfterTextEdit(t.oldRange,t.newRange))}setActive(t){this._active=t}get propertyText(){return void 0!==this.text?this.text:""===this.name?"":this.name+": "+this.value+(this.important?" !important":"")+";"}activeInStyle(){return this._active}async setText(t,e,s){if(!this.ownerStyle)return Promise.reject(new Error("No ownerStyle for property"));if(!this.ownerStyle.styleSheetId)return Promise.reject(new Error("No owner style id"));if(!this.range||!this.ownerStyle.range)return Promise.reject(new Error("Style not editable"));if(e&&(HostModule.userMetrics.actionTaken(HostModule.UserMetrics.Action.StyleRuleEdited),this.name.startsWith("--")&&HostModule.userMetrics.actionTaken(HostModule.UserMetrics.Action.CustomPropertyEdited)),s&&t===this.propertyText)return this.ownerStyle.cssModel().domModel().markUndoableState(!e),Promise.resolve(!0);const i=this.range.relativeTo(this.ownerStyle.range.startLine,this.ownerStyle.range.startColumn),n=this.ownerStyle.cssText?this._detectIndentation(this.ownerStyle.cssText):Common.Settings.Settings.instance().moduleSetting("textEditorIndent").get(),r=this.ownerStyle.cssText?n.substring(0,this.ownerStyle.range.endColumn):"",o=new TextUtils.Text.Text(this.ownerStyle.cssText||"").replaceRange(i,Platform.StringUtilities.sprintf(";%s;",t)),a=TextEditor.CodeMirrorUtils.TokenizerFactory.instance(),l=CSSProperty._formatStyle(o,n,r,a);return this.ownerStyle.setText(l,e)}static _formatStyle(t,e,s,i,n){const r=e.substring(s.length)+e;e&&(e="\n"+e);let o="",a="",l="",h=!1,m=!1;return i.createTokenizer("text/css",n)("*{"+t+"}",(function(t,s,i,n){if(!h){const i=s&&s.includes("css-comment")&&function(t){const e=t.indexOf(":");if(-1===e)return!1;const s=t.substring(2,e).trim();return cssMetadata().isCSSPropertyName(s)}(t),n=s&&(s.includes("css-string")||s.includes("css-meta")||s.includes("css-property")||s.includes("css-variable-2"));return i?o=o.trimRight()+e+t:n?(h=!0,l=t):(";"!==t||m)&&(o+=t,!t.trim()||s&&s.includes("css-comment")||(m=";"!==t)),void("{"!==t||s||(m=!1))}if("}"===t||";"===t){const s=l.trim();o=o.trimRight()+e+s+(s.endsWith(":")?" ":"")+";",m=!1,h=!1,a="","}"===t&&(o+="}")}else{if(cssMetadata().isGridAreaDefiningProperty(a)){const e=GridAreaRowRegex.exec(t);e&&0===e.index&&!l.trimRight().endsWith("]")&&(l=l.trimRight()+"\n"+r)}a||":"!==t||(a=l),l+=t}})),h&&(o+=l),o=o.substring(2,o.length-1).trimRight(),o+(e?"\n"+s:"")}_detectIndentation(t){const e=t.split("\n");return e.length<2?"":TextUtils.TextUtils.Utils.lineIndent(e[1])}setValue(t,e,s,i){const n=this.name+": "+t+(this.important?" !important":"")+";";this.setText(n,e,s).then(i)}setDisabled(t){if(!this.ownerStyle)return Promise.resolve(!1);if(t===this.disabled)return Promise.resolve(!0);if(!this.text)return Promise.resolve(!0);const e=this.text.trim(),s=t?"/* "+e+" */":this.text.substring(2,e.length-2).trim();return this.setText(s,!0,!0)}setDisplayedStringForInvalidProperty(t){this._invalidString=t}getInvalidStringForInvalidProperty(){return this._invalidString}}