import "../../chunks/chunk.QU3DSPNU.js";
import { unsafeCSS } from "lit";
import { property } from "lit/decorators.js";
import { parse, toCss, tokenize } from "../helpers/style-expression-parser.js";
const styleCache = /* @__PURE__ */ new Map();
function styleExpressionProperty(options) {
  return (proto, descriptor) => {
    const prop = options?.property ?? String(descriptor);
    const valueTemplate = options?.valueTemplate;
    const styleTemplate = options?.styleTemplate;
    const cacheKey = options?.cacheOverrideKey ?? `0`;
    const attributeName = options?.attribute ?? String(descriptor);
    property({
      attribute: attributeName,
      reflect: options?.reflect,
      noAccessor: true
    })(proto, descriptor);
    Object.defineProperty(proto, descriptor, {
      get: function() {
        return this["__" + String(descriptor)];
      },
      set: async function(newValue) {
        newValue = newValue?.toString().trim() ?? "";
        this["__" + String(descriptor)] = newValue;
        if (options?.reflect) {
          if (!newValue) {
            this.removeAttribute(attributeName);
          } else {
            this.setAttribute(attributeName, String(newValue));
          }
        }
        await this.updateComplete;
        const sel = options?.selector ?? this.constructor.styleExpressionBaseSelector;
        const lvl = this.level ?? "0";
        const styleKey = sel + prop + newValue + lvl + cacheKey;
        if (styleCache.has(styleKey)) {
          ;
          this._dynamicStylesController.inject(
            `sep_${String(descriptor)}`,
            styleCache.get(styleKey)
          );
          return;
        }
        const ast = parse(tokenize(newValue));
        const css = toCss(
          sel,
          prop,
          ast,
          valueTemplate?.bind(this),
          styleTemplate?.bind(this)
        );
        const style = unsafeCSS(css);
        styleCache.set(styleKey, style);
        this._dynamicStylesController.inject(
          `sep_${String(descriptor)}`,
          style
        );
      }
    });
  };
}
export {
  styleExpressionProperty
};
