'use strict';

const lit = require('lit');
const decorators_js = require('lit/decorators.js');
const litHtml = require('lit-html');
const tailwindVariants = require('tailwind-variants');
const web3Utils = require('web3-utils');
const viem = require('viem');
const index = require('./index-BBPgtFEP.js');
const index$1 = require('./index-DvBRl_q4.js');
require('../tailwind-config.cjs');
require('./index-oDo-wiUs.js');
const cn = require('./cn-JMx_imgS.js');

const sliceAddress = (address, startSliceBy = 6, endSliceBy) => {
  if (!address || typeof address !== "string") {
    return "";
  }
  let sliceAddress2 = "";
  const effectiveEndSliceBy = endSliceBy || startSliceBy;
  if (address.length < startSliceBy + effectiveEndSliceBy) {
    return address;
  }
  sliceAddress2 = address.length > startSliceBy + 2 ? `${address.slice(0, startSliceBy + 2)}...${address.slice(
    -effectiveEndSliceBy
  )}` : address;
  return sliceAddress2;
};

const style = ":host {\n    display: inline-flex\n}";

var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __decorateClass = (decorators, target, key, kind) => {
  var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
  for (var i = decorators.length - 1, decorator; i >= 0; i--)
    if (decorator = decorators[i])
      result = (kind ? decorator(target, key, result) : decorator(result)) || result;
  if (kind && result) __defProp(target, key, result);
  return result;
};
exports.LuksoUsername = class LuksoUsername extends index.TailwindStyledElement(style) {
  constructor() {
    super(...arguments);
    this.name = "";
    this.address = "";
    this.maxWidth = 200;
    this.size = "large";
    this.sliceBy = 8;
    this.addressColor = "";
    this.nameColor = "";
    this.customClass = "";
    this.hidePrefix = false;
    this.prefix = "@";
    this.noTransform = false;
    /** Width of the first 4 bytes of the address */
    this.bytesWidth = 52;
    this.styles = tailwindVariants.tv({
      slots: {
        wrapper: "inline-flex",
        name: "inline-block whitespace-nowrap overflow-hidden text-ellipsis",
        address: "inline-block",
        bytes: "inline-block"
      },
      variants: {
        size: {
          "2x-small": {
            wrapper: "paragraph-ptmono-10-bold"
          },
          "x-small": {
            wrapper: "paragraph-ptmono-10-bold"
          },
          small: {
            wrapper: "paragraph-ptmono-12-bold"
          },
          medium: {
            wrapper: "paragraph-ptmono-14-bold"
          },
          large: {
            wrapper: "paragraph-ptmono-16-bold"
          },
          "x-large": {
            wrapper: "paragraph-ptmono-24-bold"
          },
          "2x-large": {
            wrapper: "paragraph-ptmono-24-bold"
          }
        },
        hasNameColor: {
          false: {
            name: "text-transparent bg-clip-text bg-gradient-to-r from-gradient-1-start to-gradient-1-end"
          }
        },
        hasAddressColor: {
          false: {
            address: "text-neutral-20",
            bytes: "text-neutral-60"
          }
        }
      }
    });
  }
  transformName(name) {
    if (this.noTransform) {
      return name;
    }
    return name?.toLowerCase();
  }
  transformAddress(address) {
    if (this.noTransform || !viem.isAddress(address)) {
      return address;
    }
    try {
      return web3Utils.toChecksumAddress(address);
    } catch (error) {
      console.warn(error);
      return address;
    }
  }
  addressBytesTemplate(styles) {
    const address = this.transformAddress(this.address);
    return lit.html`<div
      class=${styles}
      style=${index$1.customStyleMap({
      [`color: var(--${this.addressColor})`]: this.addressColor !== ""
    })}
    >
      #${address.slice(2, 6)}
    </div>`;
  }
  nameTemplate(styles) {
    const name = this.transformName(this.name);
    return lit.html`<div
      class=${styles}
      style=${index$1.customStyleMap({
      [`max-width: ${this.maxWidth - (this.address ? this.bytesWidth : 0)}px`]: true,
      [`color: var(--${this.nameColor})`]: this.nameColor !== ""
    })}
    >
      ${this.hidePrefix ? litHtml.nothing : this.prefix}${name}
    </div>`;
  }
  addressTemplate(styles) {
    const address = this.transformAddress(this.address);
    return lit.html`<div
      class=${styles}
      style=${index$1.customStyleMap({
      [`color: var(--${this.addressColor})`]: this.addressColor !== ""
    })}
    >
      ${sliceAddress(address, this.sliceBy, this.sliceBy)}
    </div>`;
  }
  render() {
    const { wrapper, name, address, bytes } = this.styles({
      size: this.size,
      hasNameColor: this.nameColor !== "",
      hasAddressColor: this.addressColor !== ""
    });
    const template = (() => {
      if (this.name && this.address) {
        return lit.html`${this.nameTemplate(name())}${this.addressBytesTemplate(
          bytes()
        )}`;
      }
      if (this.name) {
        return this.nameTemplate(name());
      }
      if (this.address) {
        return this.addressTemplate(address());
      }
    })();
    return lit.html`<div class=${cn.cn(wrapper(), this.customClass)}>${template}</div>`;
  }
};
__decorateClass([
  decorators_js.property({ type: String })
], exports.LuksoUsername.prototype, "name", 2);
__decorateClass([
  decorators_js.property({ type: String })
], exports.LuksoUsername.prototype, "address", 2);
__decorateClass([
  decorators_js.property({ type: Number, attribute: "max-width" })
], exports.LuksoUsername.prototype, "maxWidth", 2);
__decorateClass([
  decorators_js.property({ type: "string" })
], exports.LuksoUsername.prototype, "size", 2);
__decorateClass([
  decorators_js.property({ type: Number, attribute: "slice-by" })
], exports.LuksoUsername.prototype, "sliceBy", 2);
__decorateClass([
  decorators_js.property({ type: String, attribute: "address-color" })
], exports.LuksoUsername.prototype, "addressColor", 2);
__decorateClass([
  decorators_js.property({ type: String, attribute: "name-color" })
], exports.LuksoUsername.prototype, "nameColor", 2);
__decorateClass([
  decorators_js.property({ type: String, attribute: "custom-class" })
], exports.LuksoUsername.prototype, "customClass", 2);
__decorateClass([
  decorators_js.property({ type: Boolean, attribute: "hide-prefix" })
], exports.LuksoUsername.prototype, "hidePrefix", 2);
__decorateClass([
  decorators_js.property({ type: String })
], exports.LuksoUsername.prototype, "prefix", 2);
__decorateClass([
  decorators_js.property({ type: Boolean, attribute: "no-transform" })
], exports.LuksoUsername.prototype, "noTransform", 2);
exports.LuksoUsername = __decorateClass([
  index.safeCustomElement("lukso-username")
], exports.LuksoUsername);

exports.sliceAddress = sliceAddress;
