'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-58ea899e.js');
const requiredFieldRule = require('./required-field-rule-d73c1d8c.js');
const accessibility = require('./accessibility-88f8af84.js');
const component = require('./component-1e352960.js');
const ui = require('./ui-3727d685.js');
const formFieldControllerSlimmer = require('./form-field-controller-slimmer-44091874.js');
const globalSlimmerRegistry = require('./global-slimmer-registry-b3bce7e0.js');
const componentUsageRuntimeMetrics = require('./component-usage-runtime-metrics-389d1548.js');
const staticSubjectTitle = require('./static-subject-title-5829a298.js');
const eventEmitSlimmer = require('./event-emit-slimmer-73c42d7e.js');
const changeManager = require('./change-manager-a297e4d2.js');
const vegaEventId = require('./vega-event-id-f95480b7.js');
require('./internal-form-field-validation-rule.abstract-07cdff46.js');
require('./string-39438062.js');
require('./observer-f49483e8.js');
require('./misc-3d30df91.js');
require('./dynamic-slimmer-f31fdfd7.js');
require('./dom-node-subject-observer-factory-ba6a95f3.js');
require('./vega-internal-event-id-e138d4d6.js');
require('./internal-translation-controller-f0da24b2.js');
require('./index-592b4052.js');
require('./try-get-document-c0ebd39a.js');
require('./page-resize-observer-slimmer-273d62ef.js');
require('./breakpoints-1cc2c4c6.js');
require('./ui-32120990.js');
require('./breakpoints-ebde4837.js');
require('./mutation-observer-slimmer-749d5020.js');
require('./mutation-observer-filters-38cebc6d.js');
require('./array-249289f2.js');
require('./number-4816603d.js');
require('./sub-state-observer-slimmer-aa93ab4f.js');
require('./internal-vega-event-manager-063c17cb.js');

const VegaColorPickerRuntimeMetricsPayloadDefinition = [
  {
    payloadKey: 'AUTO_VALIDATION_IS_TRUE',
    payloadDescription: 'Determines whether automatic validation should be implemented for the color picker when a color is selected',
    /**
     * The function checks if the `autoValidation` property of the `host` element is equal to 'true'.
     * If it is, the function will return `true`.
     *
     * @param {HTMLElement} host - The element to check
     * @returns {boolean} A boolean value
     */
    payloadValue: (host) => host.autoValidation === true,
  },
  {
    payloadKey: 'AUTO_VALIDATION_IS_FALSE',
    payloadDescription: 'Determines whether automatic validation should be implemented for the color picker when a color is selected',
    /**
     * The function checks if the `autoValidation` property of the `host` element is equal to 'false'.
     * If it is, the function will return `true`.
     *
     * @param {HTMLElement} host - The element to check
     * @returns {boolean} A boolean value
     */
    payloadValue: (host) => host.autoValidation === false,
  },
];

const vegaColorPickerCss = ":host{display:inline-block;text-align:left;line-height:normal}:host vega-field-label{margin-bottom:8px}:host(.disabled) .color-picker-inline{background-color:rgba(var(--v-bg-secondary, 245, 247, 247, 1));border:1px solid rgba(var(--v-border-input-field-disabled, 222, 225, 227, 1))}:host(.disabled) .color-picker-inline-items:hover{box-shadow:none}:host(.disabled) .color-picker-inline-items:focus-within{box-shadow:none}:host .color-picker-inline{border:1px solid rgba(var(--v-border-input-field, 171, 198, 216, 1));border-radius:8px;background-color:rgba(var(--v-bg-primary, 252, 252, 252, 1));padding:12px;max-width:max-content}:host .color-picker-inline-items{display:flex;border:1px solid rgba(var(--v-border-chip, 205, 209, 211, 1));border-radius:4px;width:24px;height:24px;cursor:pointer;align-items:center;justify-content:center}:host .color-picker-inline-items.selected{border-color:rgba(var(--v-bg-inverted-primary, 4, 4, 28, 1))}:host .color-picker-inline-items:hover{box-shadow:0 0 0 4px rgba(var(--v-ring-input-focus, 19, 98, 226, 0.25))}:host .color-picker-inline-items:focus-visible{outline:2px solid rgba(var(--v-border-color-action, 19, 98, 226, 1));outline-offset:0px;border:1px solid rgba(var(--v-border-color-input-field-focus, 19, 98, 226, 1));box-shadow:0px 0px 0px 1px rgba(var(--v-bg-primary, 252, 252, 252, 1)) inset;border:1px solid rgba(var(--v-bg-primary, 252, 252, 252, 1))}:host .checkmark-black{color:rgba(var(--v-text-black, 32, 54, 69, 1))}:host .checkmark-white{color:rgba(var(--v-text-white, 252, 252, 252, 1))}:host vega-text{padding-left:8px;padding-right:8px;padding-top:4px;font-family:\"Inter\", sans-serif;font-size:14px;font-weight:400;line-height:18px;letter-spacing:0px;color:rgba(var(--v-text-secondary, 107, 116, 125, 1))}@media screen and (min-width: 768px) and (max-width: 1023px){:host vega-text{font-family:\"Inter\", sans-serif;font-size:14px;font-weight:400;line-height:18px;letter-spacing:0px}}@media screen and (min-width: 1024px) and (max-width: 1439px){:host vega-text{font-family:\"Inter\", sans-serif;font-size:14px;font-weight:400;line-height:18px;letter-spacing:0px}}@media screen and (min-width: 1440px) and (max-width: 9999px){:host vega-text{font-family:\"Inter\", sans-serif;font-size:14px;font-weight:400;line-height:18px;letter-spacing:0px}}:host .vega-error{display:inline-block;padding-left:8px;padding-right:8px;padding-top:8px;font-family:\"Inter\", sans-serif;font-size:14px;font-weight:400;line-height:18px;letter-spacing:0px;color:rgba(var(--v-text-error, 189, 41, 71, 1))}@media screen and (min-width: 768px) and (max-width: 1023px){:host .vega-error{font-family:\"Inter\", sans-serif;font-size:14px;font-weight:400;line-height:18px;letter-spacing:0px}}@media screen and (min-width: 1024px) and (max-width: 1439px){:host .vega-error{font-family:\"Inter\", sans-serif;font-size:14px;font-weight:400;line-height:18px;letter-spacing:0px}}@media screen and (min-width: 1440px) and (max-width: 9999px){:host .vega-error{font-family:\"Inter\", sans-serif;font-size:14px;font-weight:400;line-height:18px;letter-spacing:0px}}:host .color-picker-inline.error{border:1px solid rgba(var(--v-border-input-field-danger, 230, 50, 87, 1))}:host .vega-hidden{display:none}:host vega-flex{flex-wrap:wrap}";

var __decorate = (undefined && undefined.__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;
};
const VegaColorPicker = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.vegaChange = index.createEvent(this, "vegaChange", 7);
    this.change = index.createEvent(this, "change", 7);
    this.globalSlimmers = {};
    this.formFieldController = new formFieldControllerSlimmer.FormFieldControllerSlimmer({
      propertyRules: [
        {
          propertyName: 'required',
          ruleFactory: (isRequired) => (isRequired ? new requiredFieldRule.RequiredFieldRule() : null),
        },
      ],
      validationRulesName: 'validationRules',
      defaultValue: '',
      attributeGetter: (propertyName) => this[propertyName],
      inputContainerGetter: () => this.colorPickerContainerRef,
      touchableAreaGetter: () => [this.colorPickerTouchableAreaRef],
    });
    this.changeEventEmitter = eventEmitSlimmer.createEventEmitSlimmer(VegaColorPicker, vegaEventId.VegaChange);
    this.vegaComponentUsageRuntimeMetricsSlimmer = new componentUsageRuntimeMetrics.VegaComponentUsageRuntimeMetricsSlimmer(VegaColorPickerRuntimeMetricsPayloadDefinition);
    /**
     * Specifies the initial or preset color value of the color picker.
     *    It is represented as a hexadecimal string.
     *
     * @vegaVersion 1.13.0
     */
    /* eslint-disable-next-line @stencil/strict-mutable */
    this.value = '';
    /**
     * Displays a supplementary prompt message that assists users
     *    in understanding the expected value for the color picker.
     *
     * @vegaVersion 1.13.0
     */
    this.hint = '';
    /**
     * Specifies whether the color picker element is disabled.
     *
     * @vegaVersion 1.13.0
     */
    this.disabled = false;
    /**
     * Specifies whether a color must be selected for
     *    the color picker to pass form validation.
     *
     * @vegaVersion 1.13.0
     */
    this.required = false;
    /**
     * Specifies the label or title for the color picker.
     *
     * It provides descriptive text that helps identify
     *    the purpose or content expected in the color picker.
     *
     * @vegaVersion 1.13.0
     */
    this.label = '';
    /**
     * Specifies the validation status of the color picker component.
     *
     * @vegaVersion 1.13.0
     */
    /* eslint-disable-next-line @stencil/strict-mutable */
    this.isValid = null;
    /**
     * Determines whether automatic validation should be implemented
     *  for the color picker when a color is selected.
     *
     * @vegaVersion 1.13.0
     */
    this.autoValidation = true;
    /**
     * Specifies custom validation rules for the color picker.
     *
     * @vegaVersion 1.13.0
     */
    this.validationRules = [];
    /**
     * Defines custom color options for the color picker.
     *
     * It should be an array of hexadecimal strings representing different colors.
     *
     * @vegaVersion 1.13.0
     */
    this.colors = [
      '#73E6DC',
      '#806BFF',
      '#FFDC83',
      '#FF9571',
      '#00BBFF',
      '#FF82C9',
      '#259F9F',
      '#563D82',
    ];
    /**
     * Specifies whether the color picker is inline with container styles, including a border, background color, and padding.
     *
     * @vegaVersion 2.30.0
     */
    this.isInline = false;
    /**
     * Specifies the maximum number of color swatches displayed per row.
     *
     * When the number of `colors` exceeds `columns`, the remaining swatches
     *    wrap to additional rows. For example, `columns={5}` with 10 colors
     *    renders 2 rows of 5 swatches.
     *
     * Defaults to `8` to preserve the original single-row layout for the
     *    default 8-color palette.
     *
     * @vegaVersion 2.82.0
     */
    this.columns = 8;
    this.renderColorPickerContainer = () => {
      const columns = this.getValidColumns();
      return (index.h("div", { class: ui.arrayToClassString([!this.isInline ? 'color-picker-inline' : null]), ref: (e) => (this.colorPickerContainerRef = e) }, index.h("vega-flex", { direction: "row", gap: "size-8", alignItems: "center", style: { maxWidth: `${columns * 34 - 8}px` } }, this.colors.map((color) => {
        const selected = this.value === color;
        return (index.h("div", { id: color, class: { 'color-picker-inline-items': true, 'selected': selected }, ref: (e) => (this.colorPickerTouchableAreaRef = e), style: {
            backgroundColor: color,
          }, onClick: this.updateValueProp, role: 'button', tabIndex: 0, "aria-label": "color", onKeyDown: accessibility.createEnterKeyHandlerToTriggerClick() }, selected ? (index.h("vega-icon", { icon: "checkmark", size: "size-16", class: {
            'v-text-black': this.isDarkColorCheckmark(this.value),
            'v-text-white': !this.isDarkColorCheckmark(this.value),
          } })) : null));
      }))));
    };
    this.renderHintMessage = () => {
      return index.h("vega-text", null, this.hint);
    };
    this.updateValueProp = async (e) => {
      if (!this.disabled) {
        this.value = String(e.currentTarget.getAttribute('id'));
      }
    };
  }
  watchValue() {
    this.resetValueIfInvalid();
    this.changeEventEmitter.emit(this.value);
    changeManager.ChangeManager.notify(staticSubjectTitle.FORM_ELEMENT_VALUE_CHANGE, {
      host: this.host,
      detail: this.value,
    });
  }
  watchValidationRules() {
  }
  connectedCallback() {
    this.resetValueIfInvalid();
  }
  render() {
    return component.sanitizeVegaComponent(index.h(index.Host, { class: {
        disabled: this.disabled,
      } }, this.renderLabelDom(), this.renderColorPickerContainer(), this.hint && this.renderHintMessage()), this.host);
  }
  renderLabelDom() {
    return (index.h("vega-field-label", { "is-field-required": this.required, disabled: this.disabled, label: this.label }));
  }
  getValidColumns() {
    const flooredColumns = Math.floor(this.columns);
    return Number.isFinite(flooredColumns) && flooredColumns > 0 ? flooredColumns : 1;
  }
  // W3C Recommendations for color contrast rules, please check the following link for more details:
  // https://stackoverflow.com/questions/3942878/how-to-decide-font-color-in-white-or-black-depending-on-background-color
  isDarkColorCheckmark(bgColor, lightColor = '#fcfcfc', darkColor = '#203645') {
    const L = this.getLuminance(bgColor);
    const L1 = this.getLuminance(lightColor);
    const L2 = this.getLuminance(darkColor);
    return L > Math.sqrt((L1 + 0.05) * (L2 + 0.05)) - 0.05;
  }
  getLuminance(hexColor) {
    const r = ui.hexToRGB(hexColor).r; // hexToR
    const g = ui.hexToRGB(hexColor).g; // hexToG
    const b = ui.hexToRGB(hexColor).b; // hexToB
    const colors = [r / 255, g / 255, b / 255];
    const c = colors.map((col) => col <= 0.03928 ? col / 12.92 : ((col + 0.055) / 1.055) ** 2.4);
    return 0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2];
  }
  resetValueIfInvalid() {
    const colorExistInList = this.colors.some((color) => {
      return ui.getHexFromShorthand(color).toUpperCase() == ui.getHexFromShorthand(this.value).toUpperCase();
    });
    if (!colorExistInList) {
      this.value = '';
    }
  }
  get host() { return index.getElement(this); }
  static get watchers() { return {
    "value": ["watchValue"],
    "validationRules": ["watchValidationRules"]
  }; }
};
__decorate([
  componentUsageRuntimeMetrics.InjectVegaGlobalSlimmer()
], VegaColorPicker.prototype, "globalSlimmers", void 0);
__decorate([
  globalSlimmerRegistry.InjectVegaSlimmer()
], VegaColorPicker.prototype, "formFieldController", void 0);
__decorate([
  globalSlimmerRegistry.InjectVegaSlimmer()
], VegaColorPicker.prototype, "changeEventEmitter", void 0);
__decorate([
  globalSlimmerRegistry.InjectVegaSlimmer()
], VegaColorPicker.prototype, "vegaComponentUsageRuntimeMetricsSlimmer", void 0);
VegaColorPicker.style = vegaColorPickerCss;

exports.vega_color_picker = VegaColorPicker;
