var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { MapToComponentField, VegaSlimmer } from 'vega-slimmer/core';
import { h } from '@stencil/core';
import { DomNodeSubjectObserverFactory } from '../../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-observer-factory';
import { VegaClick } from '../../../../../helpers/event-manager/event-id/vega-event-id';
/**
 * Renderer for the color swatch list in the color swatch picker popover.
 */
export class ColorSwatchGridRenderer extends VegaSlimmer {
  constructor() {
    super(...arguments);
    /**
     * Handle for adding click listener to each color swatch in the grid.
     *
     * @param {HTMLVegaColorSwatchElement} el - The color swatch element.
     */
    this.handleColorSwatchClick = (el) => {
      DomNodeSubjectObserverFactory.addUniqueObserverToNode(el, VegaClick, () => {
        const color = el.color;
        this.colorSwatchPickerValueController.updateInternalColor(color);
      });
    };
  }
  render() {
    return (h("vega-grid", { column: this.swatchGridColumns, justifyItems: "start", gap: "size-8", style: { width: 'max-content' } }, this.colors.map((color) => {
      return (h("vega-color-swatch", { ref: this.handleColorSwatchClick, selected: this.isColorMatched(this.internalColor, color), color: color, size: "extra-small" }));
    })));
  }
  /**
   * Checks if two colors match, ignoring case.
   *
   * @param {Nullable<string>} color1 - The first color to compare.
   * @param {Nullable<string>} color2 - The second color to compare.
   * @returns {boolean} True if the colors match, false otherwise.
   */
  isColorMatched(color1, color2) {
    if (color1 && color2) {
      return color1.toLowerCase() === color2.toLowerCase();
    }
    return color1 === color2;
  }
}
__decorate([
  MapToComponentField()
], ColorSwatchGridRenderer.prototype, "colors", void 0);
__decorate([
  MapToComponentField()
], ColorSwatchGridRenderer.prototype, "internalColor", void 0);
__decorate([
  MapToComponentField()
], ColorSwatchGridRenderer.prototype, "swatchGridColumns", void 0);
__decorate([
  MapToComponentField()
], ColorSwatchGridRenderer.prototype, "colorSwatchPickerValueController", void 0);
