import*as i18n from"../../../core/i18n/i18n.js";import inspectorCommonStyles from"../../legacy/inspectorCommon.css.js";import*as LitHtml from"../../lit-html/lit-html.js";import*as ComponentHelpers from"../helpers/helpers.js";import*as IconButton from"../icon_button/icon_button.js";import valueInterpreterDisplayStyles from"./valueInterpreterDisplay.css.js";import{format,getDefaultValueTypeMapping,getPointerAddress,isNumber,isPointer,isValidMode,VALUE_TYPE_MODE_LIST}from"./ValueInterpreterDisplayUtils.js";const UIStrings={unsignedValue:"`Unsigned` value",changeValueTypeMode:"Change mode",signedValue:"`Signed` value",jumpToPointer:"Jump to address",addressOutOfRange:"Address out of memory range"},str_=i18n.i18n.registerUIStrings("ui/components/linear_memory_inspector/ValueInterpreterDisplay.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_),{render:render,html:html}=LitHtml,SORTED_VALUE_TYPES=Array.from(getDefaultValueTypeMapping().keys());export class ValueTypeModeChangedEvent extends Event{static eventName="valuetypemodechanged";data;constructor(e,t){super(ValueTypeModeChangedEvent.eventName,{composed:!0}),this.data={type:e,mode:t}}}export class JumpToPointerAddressEvent extends Event{static eventName="jumptopointeraddress";data;constructor(e){super(JumpToPointerAddressEvent.eventName,{composed:!0}),this.data=e}}export class ValueInterpreterDisplay extends HTMLElement{static litTagName=LitHtml.literal`devtools-linear-memory-inspector-interpreter-display`;#e=this.attachShadow({mode:"open"});#t="Little Endian";#n=new ArrayBuffer(0);#s=new Set;#i=getDefaultValueTypeMapping();#r=0;constructor(){super(),this.#e.adoptedStyleSheets=[inspectorCommonStyles]}connectedCallback(){this.#e.adoptedStyleSheets=[valueInterpreterDisplayStyles]}set data(e){this.#n=e.buffer,this.#t=e.endianness,this.#s=e.valueTypes,this.#r=e.memoryLength,e.valueTypeModes&&e.valueTypeModes.forEach(((e,t)=>{isValidMode(t,e)&&this.#i.set(t,e)})),this.#a()}#a(){render(html`
      <div class="value-types">
        ${SORTED_VALUE_TYPES.map((e=>this.#s.has(e)?this.#o(e):""))}
      </div>
    `,this.#e,{host:this})}#o(e){if(isNumber(e))return this.#l(e);if(isPointer(e))return this.#d(e);throw new Error(`No known way to format ${e}`)}#d(e){const t=this.#p({type:e,signed:!1}),n=getPointerAddress(e,this.#n,this.#t),s=Number.isNaN(n)||BigInt(n)>=BigInt(this.#r),i=i18nString(s?UIStrings.addressOutOfRange:UIStrings.jumpToPointer),r=s?"var(--icon-default)":"var(--icon-link)";return html`
      <span class="value-type-cell-no-mode value-type-cell selectable-text">${i18n.i18n.lockedString(e)}</span>
      <div class="value-type-cell">
        <div class="value-type-value-with-link" data-value="true">
        <span class="selectable-text">${t}</span>
          ${html`
              <button class="jump-to-button" data-jump="true" title=${i} ?disabled=${s}
                @click=${this.#u.bind(this,Number(n))}>
                <${IconButton.Icon.Icon.litTagName} .data=${{iconName:"open-externally",color:r,width:"16px"}}>
                </${IconButton.Icon.Icon.litTagName}>
              </button>`}
        </div>
      </div>
    `}#u(e){this.dispatchEvent(new JumpToPointerAddressEvent(e))}#l(e){return html`
      <span class="value-type-cell selectable-text">${i18n.i18n.lockedString(e)}</span>
      <div>
        <select title=${i18nString(UIStrings.changeValueTypeMode)}
          data-mode-settings="true"
          class="chrome-select"
          style="border: none; background-color: transparent; cursor: pointer; color: var(--color-text-secondary);"
          @change=${this.#c.bind(this,e)}>
            ${VALUE_TYPE_MODE_LIST.filter((t=>isValidMode(e,t))).map((t=>html`
                <option value=${t} .selected=${this.#i.get(e)===t}>${i18n.i18n.lockedString(t)}
                </option>`))}
        </select>
      </div>
      ${this.#m(e)}
    `}#m(e){const t=this.#p({type:e,signed:!1}),n=this.#p({type:e,signed:!0}),s=this.#i.get(e),i=n!==t&&"hex"!==s&&"oct"!==s,r=html`<span class="value-type-cell selectable-text"  title=${i18nString(UIStrings.unsignedValue)} data-value="true">${t}</span>`;if(!i)return r;const a="Integer 32-bit"===e||"Integer 64-bit"===e,o=html`<span class="selectable-text" data-value="true" title=${i18nString(UIStrings.signedValue)}>${n}</span>`;return a?html`
        <div class="value-type-cell">
          ${r}
          ${o}
        </div>
        `:html`
      <div class="value-type-cell" style="flex-direction: row;">
        ${r}
        <span class="signed-divider"></span>
        ${o}
      </div>
    `}#c(e,t){t.preventDefault();const n=t.target.value;this.dispatchEvent(new ValueTypeModeChangedEvent(e,n))}#p(e){const t=this.#i.get(e.type);return format({buffer:this.#n,type:e.type,endianness:this.#t,signed:e.signed||!1,mode:t})}}ComponentHelpers.CustomElements.defineComponent("devtools-linear-memory-inspector-interpreter-display",ValueInterpreterDisplay);