import*as Common from"../common/common.js";import*as i18n from"../i18n/i18n.js";import*as TextUtils from"../text_utils/text_utils.js";import{DeferredDOMNode}from"./DOMModel.js";import{ResourceTreeModel}from"./ResourceTreeModel.js";const UIStrings={couldNotFindTheOriginalStyle:"Could not find the original style sheet.",thereWasAnErrorRetrievingThe:"There was an error retrieving the source styles."},str_=i18n.i18n.registerUIStrings("sdk/CSSStyleSheetHeader.js",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class CSSStyleSheetHeader{constructor(e,t){this._cssModel=e,this.id=t.styleSheetId,this.frameId=t.frameId,this.sourceURL=t.sourceURL,this.hasSourceURL=Boolean(t.hasSourceURL),this.origin=t.origin,this.title=t.title,this.disabled=t.disabled,this.isInline=t.isInline,this.isMutable=t.isMutable,this.isConstructed=t.isConstructed,this.startLine=t.startLine,this.startColumn=t.startColumn,this.endLine=t.endLine,this.endColumn=t.endColumn,this.contentLength=t.length,t.ownerNode&&(this.ownerNode=new DeferredDOMNode(e.target(),t.ownerNode)),this.sourceMapURL=t.sourceMapURL,this._originalContentProvider=null}originalContentProvider(){if(!this._originalContentProvider){const e=async()=>{const e=await this._cssModel.originalStyleSheetText(this);return null===e?{content:null,error:i18nString(UIStrings.couldNotFindTheOriginalStyle),isEncoded:!1}:{content:e,isEncoded:!1}};this._originalContentProvider=new TextUtils.StaticContentProvider.StaticContentProvider(this.contentURL(),this.contentType(),e)}return this._originalContentProvider}setSourceMapURL(e){this.sourceMapURL=e}cssModel(){return this._cssModel}isAnonymousInlineStyleSheet(){return!this.resourceURL()&&!this._cssModel.sourceMapManager().sourceMapForClient(this)}resourceURL(){return this.isViaInspector()?this._viaInspectorResourceURL():this.sourceURL}_viaInspectorResourceURL(){const e=this._cssModel.target().model(ResourceTreeModel);if(console.assert(Boolean(e)),!e)return"";const t=e.frameForId(this.frameId);if(!t)return"";console.assert(Boolean(t));const r=new Common.ParsedURL.ParsedURL(t.url);let n="inspector://"+r.host+r.folderPathComponents;return n.endsWith("/")||(n+="/"),n+="inspector-stylesheet",n}lineNumberInSource(e){return this.startLine+e}columnNumberInSource(e,t){return(e?0:this.startColumn)+t}containsLocation(e,t){const r=e===this.startLine&&t>=this.startColumn||e>this.startLine,n=e<this.endLine||e===this.endLine&&t<=this.endColumn;return r&&n}contentURL(){return this.resourceURL()}contentType(){return Common.ResourceType.resourceTypes.Stylesheet}contentEncoded(){return Promise.resolve(!1)}async requestContent(){try{return{content:await this._cssModel.getStyleSheetText(this.id),isEncoded:!1}}catch(e){return{content:null,error:i18nString(UIStrings.thereWasAnErrorRetrievingThe),isEncoded:!1}}}async searchInContent(e,t,r){const n=await this.requestContent();return null===n.content?[]:TextUtils.TextUtils.performSearchInContent(n.content,e,t,r)}isViaInspector(){return"inspector"===this.origin}createPageResourceLoadInitiator(){return{target:null,frameId:this.frameId,initiatorUrl:this.hasSourceURL?"":this.sourceURL}}}