import*as SDK from"../sdk/sdk.js";export const fullQualifiedSelector=function(e,t){return e.nodeType()!==Node.ELEMENT_NODE?e.localName()||e.nodeName().toLowerCase():cssPath(e,t)};export const cssPath=function(e,t){if(e.nodeType()!==Node.ELEMENT_NODE)return"";const o=[];let n=e;for(;n;){const r=_cssPathStep(n,Boolean(t),n===e);if(!r)break;if(o.push(r),r.optimized)break;n=n.parentNode}return o.reverse(),o.join(" > ")};export const canGetJSPath=function(e){let t=e;for(;t;){const e=t.ancestorShadowRoot();if(e&&e.shadowRootType()!==SDK.DOMModel.DOMNode.ShadowRootTypes.Open)return!1;t=t.ancestorShadowHost()}return!0};export const jsPath=function(e,t){if(e.nodeType()!==Node.ELEMENT_NODE)return"";const o=[];let n=e;for(;n;)o.push(cssPath(n,t)),n=n.ancestorShadowHost();o.reverse();let r="";for(let s=0;s<o.length;++s){const e=JSON.stringify(o[s]);r+=s?`.shadowRoot.querySelector(${e})`:`document.querySelector(${e})`}return r};export const _cssPathStep=function(e,t,o){if(e.nodeType()!==Node.ELEMENT_NODE)return null;const n=e.getAttribute("id");if(t){if(n)return new Step(c(n),!0);const t=e.nodeName().toLowerCase();if("body"===t||"head"===t||"html"===t)return new Step(e.nodeNameInCorrectCase(),!0)}const r=e.nodeNameInCorrectCase();if(n)return new Step(r+c(n),!0);const s=e.parentNode;if(!s||s.nodeType()===Node.DOCUMENT_NODE)return new Step(r,!0);function i(e){const t=e.getAttribute("class");return t?t.split(/\s+/g).filter(Boolean).map((function(e){return"$"+e})):[]}function c(e){return"#"+CSS.escape(e)}const a=i(e);let d=!1,N=!1,u=-1,p=-1;const f=s.children();for(let E=0;f&&(-1===u||!N)&&E<f.length;++E){const t=f[E];if(t.nodeType()!==Node.ELEMENT_NODE)continue;if(p+=1,t===e){u=p;continue}if(N)continue;if(t.nodeNameInCorrectCase()!==r)continue;d=!0;const o=new Set(a);if(!o.size){N=!0;continue}const n=i(t);for(let e=0;e<n.length;++e){const t=n[e];if(o.has(t)&&(o.delete(t),!o.size)){N=!0;break}}}let l=r;if(o&&"input"===r.toLowerCase()&&e.getAttribute("type")&&!e.getAttribute("id")&&!e.getAttribute("class")&&(l+="[type="+CSS.escape(e.getAttribute("type")||"")+"]"),N)l+=":nth-child("+(u+1)+")";else if(d)for(const E of a)l+="."+CSS.escape(E.slice(1));return new Step(l,!1)};export const xPath=function(e,t){if(e.nodeType()===Node.DOCUMENT_NODE)return"/";const o=[];let n=e;for(;n;){const e=_xPathValue(n,t);if(!e)break;if(o.push(e),e.optimized)break;n=n.parentNode}return o.reverse(),(o.length&&o[0].optimized?"":"/")+o.join("/")};export const _xPathValue=function(e,t){let o;const n=_xPathIndex(e);if(-1===n)return null;switch(e.nodeType()){case Node.ELEMENT_NODE:if(t&&e.getAttribute("id"))return new Step('//*[@id="'+e.getAttribute("id")+'"]',!0);o=e.localName();break;case Node.ATTRIBUTE_NODE:o="@"+e.nodeName();break;case Node.TEXT_NODE:case Node.CDATA_SECTION_NODE:o="text()";break;case Node.PROCESSING_INSTRUCTION_NODE:o="processing-instruction()";break;case Node.COMMENT_NODE:o="comment()";break;case Node.DOCUMENT_NODE:default:o=""}return n>0&&(o+="["+n+"]"),new Step(o,e.nodeType()===Node.DOCUMENT_NODE)};export const _xPathIndex=function(e){function t(e,t){if(e===t)return!0;if(e.nodeType()===Node.ELEMENT_NODE&&t.nodeType()===Node.ELEMENT_NODE)return e.localName()===t.localName();if(e.nodeType()===t.nodeType())return!0;return(e.nodeType()===Node.CDATA_SECTION_NODE?Node.TEXT_NODE:e.nodeType())===(t.nodeType()===Node.CDATA_SECTION_NODE?Node.TEXT_NODE:t.nodeType())}const o=e.parentNode?e.parentNode.children():null;if(!o)return 0;let n;for(let s=0;s<o.length;++s)if(t(e,o[s])&&o[s]!==e){n=!0;break}if(!n)return 0;let r=1;for(let s=0;s<o.length;++s)if(t(e,o[s])){if(o[s]===e)return r;++r}return-1};export class Step{constructor(e,t){this.value=e,this.optimized=t||!1}toString(){return this.value}}