import*as TextUtils from"../text_utils/text_utils.js";import{CSSMedia}from"./CSSMedia.js";import{CSSStyleDeclaration,Type}from"./CSSStyleDeclaration.js";export class CSSRule{constructor(e,t){if(this._cssModel=e,this.styleSheetId=t.styleSheetId,this.styleSheetId){const e=this._getStyleSheetHeader(this.styleSheetId);this.sourceURL=e.sourceURL}this.origin=t.origin,this.style=new CSSStyleDeclaration(this._cssModel,this,t.style,Type.Regular)}rebase(e){this.styleSheetId===e.styleSheetId&&this.style.rebase(e)}resourceURL(){if(!this.styleSheetId)return"";return this._getStyleSheetHeader(this.styleSheetId).resourceURL()}isUserAgent(){return this.origin===Protocol.CSS.StyleSheetOrigin.UserAgent}isInjected(){return this.origin===Protocol.CSS.StyleSheetOrigin.Injected}isViaInspector(){return this.origin===Protocol.CSS.StyleSheetOrigin.Inspector}isRegular(){return this.origin===Protocol.CSS.StyleSheetOrigin.Regular}cssModel(){return this._cssModel}_getStyleSheetHeader(e){const t=this._cssModel.styleSheetHeaderForId(e);return console.assert(null!==t),t}}class CSSValue{constructor(e){this.text=e.text,e.range&&(this.range=TextUtils.TextRange.TextRange.fromObject(e.range))}rebase(e){this.range&&(this.range=this.range.rebaseAfterTextEdit(e.oldRange,e.newRange))}}export class CSSStyleRule extends CSSRule{constructor(e,t,s){super(e,{origin:t.origin,style:t.style,styleSheetId:t.styleSheetId}),this.selectors,this._reinitializeSelectors(t.selectorList),this.media=t.media?CSSMedia.parseMediaArrayPayload(e,t.media):[],this.wasUsed=s||!1}static createDummyRule(e,t){const s={selectorList:{text:"",selectors:[{text:t,value:void 0}]},style:{styleSheetId:"0",range:new TextUtils.TextRange.TextRange(0,0,0,0),shorthandEntries:[],cssProperties:[]},origin:Protocol.CSS.StyleSheetOrigin.Inspector};return new CSSStyleRule(e,s)}_reinitializeSelectors(e){this.selectors=[];for(let t=0;t<e.selectors.length;++t)this.selectors.push(new CSSValue(e.selectors[t]))}setSelectorText(e){const t=this.styleSheetId;if(!t)throw"No rule stylesheet id";const s=this.selectorRange();if(!s)throw"Rule selector is not editable";return this._cssModel.setSelectorText(t,s,e)}selectorText(){return this.selectors.map((e=>e.text)).join(", ")}selectorRange(){const e=this.selectors[0].range,t=this.selectors[this.selectors.length-1].range;return e&&t?new TextUtils.TextRange.TextRange(e.startLine,e.startColumn,t.endLine,t.endColumn):null}lineNumberInSource(e){const t=this.selectors[e];if(!t||!t.range||!this.styleSheetId)return 0;return this._getStyleSheetHeader(this.styleSheetId).lineNumberInSource(t.range.startLine)}columnNumberInSource(e){const t=this.selectors[e];if(!t||!t.range||!this.styleSheetId)return;return this._getStyleSheetHeader(this.styleSheetId).columnNumberInSource(t.range.startLine,t.range.startColumn)}rebase(e){if(this.styleSheetId!==e.styleSheetId)return;const t=this.selectorRange();if(t&&t.equal(e.oldRange))this._reinitializeSelectors(e.payload);else for(let s=0;s<this.selectors.length;++s)this.selectors[s].rebase(e);for(const s of this.media)s.rebase(e);super.rebase(e)}}export class CSSKeyframesRule{constructor(e,t){this._cssModel=e,this._animationName=new CSSValue(t.animationName),this._keyframes=t.keyframes.map((t=>new CSSKeyframeRule(e,t)))}name(){return this._animationName}keyframes(){return this._keyframes}}export class CSSKeyframeRule extends CSSRule{constructor(e,t){super(e,{origin:t.origin,style:t.style,styleSheetId:t.styleSheetId}),this._keyText,this._reinitializeKey(t.keyText)}key(){return this._keyText}_reinitializeKey(e){this._keyText=new CSSValue(e)}rebase(e){this.styleSheetId===e.styleSheetId&&this._keyText.range&&(e.oldRange.equal(this._keyText.range)?this._reinitializeKey(e.payload):this._keyText.rebase(e),super.rebase(e))}setKeyText(e){const t=this.styleSheetId;if(!t)throw"No rule stylesheet id";const s=this._keyText.range;if(!s)throw"Keyframe key is not editable";return this._cssModel.setKeyframeKey(t,s,e)}}