/*!
 * KoliBri - The accessible HTML-Standard
 */
'use strict';

var index = require('./index-DBbl8tEq.js');
var variantQuote = require('./variant-quote-DUWZtljE.js');
var i18n = require('./i18n-DL7vJXNn.js');
require('./common-DAAt5YQ7.js');
var normalizers = require('./normalizers-R90MJyQR.js');
var label = require('./label-CrUhy8f0.js');
var valueNumberClamped = require('./value-number-clamped-B6AfADe2.js');
var baseController = require('./base-controller-DAzsTqX9.js');
require('./i18n-CgUN6lev.js');
require('./_Uint8Array-BAQUGozM.js');
require('./isArray-BOIOdEQh.js');

const highProp = normalizers.createPropDefinition('high', 0, normalizers.normalizeNumber);

const lowProp = normalizers.createPropDefinition('low', 0, normalizers.normalizeNumber);

const minProp = normalizers.createPropDefinition('min', 0, normalizers.normalizeNumber);

const optimumProp = normalizers.createPropDefinition('optimum', 0, normalizers.normalizeNumber);

const orientationOptions = ['horizontal', 'vertical'];
const orientationProp = normalizers.createPropDefinition('orientation', 'horizontal', (value) => {
    const str = normalizers.normalizeString(value);
    if (orientationOptions.includes(str)) {
        return str;
    }
    throw new Error(`Invalid orientation: ${str}`);
});

const numberValueProp = normalizers.createPropDefinition('value', 0, normalizers.normalizeNumber, (v) => v >= 0);

function getMeterState(value, min, max, low, high, optimum) {
    const effectiveLow = low !== null && low !== void 0 ? low : min;
    const effectiveHigh = high !== null && high !== void 0 ? high : max;
    if (optimum === undefined) {
        const inMidRegion = value >= effectiveLow && value <= effectiveHigh;
        return inMidRegion ? 'optimum' : 'suboptimal';
    }
    const inLowRegion = value < effectiveLow;
    const inHighRegion = value > effectiveHigh;
    const inMidRegion = !inLowRegion && !inHighRegion;
    const optimumInLow = optimum < effectiveLow;
    const optimumInHigh = optimum > effectiveHigh;
    if (optimumInLow) {
        if (inLowRegion)
            return 'optimum';
        if (inMidRegion)
            return 'suboptimal';
        return 'critical';
    }
    else if (optimumInHigh) {
        if (inHighRegion)
            return 'optimum';
        if (inMidRegion)
            return 'suboptimal';
        return 'critical';
    }
    else {
        if (inMidRegion)
            return 'optimum';
        return 'suboptimal';
    }
}
const MeterFC = (props) => {
    const { high, label, low, max, min, optimum, orientation, unit, value } = props;
    const isVertical = orientation === 'vertical';
    const isPercentage = unit === '%';
    const displayValue = isPercentage ? Math.round(((value - min) / (max - min)) * 100) : value;
    const state = getMeterState(value, min, max, low, high, optimum);
    const hasStateClassification = low !== undefined || high !== undefined;
    const stateLabel = hasStateClassification ? i18n.translate(`kol-meter-state-${state}`) : '';
    const charCount = max.toString().length > min.toString().length ? max.toString().length + 'ch' : min.toString().length + 'ch';
    return (index.h("div", { class: { 'kol-meter': true, 'kol-meter--vertical': isVertical } }, index.h("div", { class: "kol-meter__bar" }, index.h("div", { class: "kol-meter__bar-label" }, label, hasStateClassification && (index.h("span", { class: `kol-meter__bar-state kol-meter__bar-state--${state}` }, ' – ', stateLabel))), index.h("div", { class: "kol-meter__bar-track" }, index.h("meter", { "aria-label": label, high: high, low: low, max: max, min: min, optimum: optimum, value: value })), index.h("span", { class: "kol-meter__value-unit" }, index.h("span", { class: "kol-meter__value", style: { 'min-width': charCount } }, displayValue), index.h("span", { class: "kol-meter__unit" }, unit)))));
};

const meterPropsConfig = {
    optional: [minProp, orientationProp, valueNumberClamped.unitProp],
    required: [label.labelProp, valueNumberClamped.maxProp, numberValueProp],
};

class MeterController extends baseController.BaseController {
    constructor(stateAccess) {
        super(stateAccess, meterPropsConfig);
        this.meterData = { high: undefined, low: undefined, optimum: undefined };
    }
    componentWillLoad(props) {
        const { high, label, low, max, min, optimum, orientation, unit, value } = props;
        this.watchHigh(high);
        this.watchLabel(label);
        this.watchLow(low);
        this.watchMax(max);
        this.watchMin(min);
        this.watchOptimum(optimum);
        this.watchOrientation(orientation);
        this.watchUnit(unit);
        this.watchValue(value);
    }
    getMeterData() {
        return this.meterData;
    }
    watchHigh(value) {
        if (value === undefined) {
            this.meterData.high = undefined;
        }
        else {
            highProp.apply(value, (v) => {
                this.meterData.high = v;
            });
        }
    }
    watchLabel(value) {
        label.labelProp.apply(value, (v) => {
            this.setRenderProp('label', v);
        });
    }
    watchLow(value) {
        if (value === undefined) {
            this.meterData.low = undefined;
        }
        else {
            lowProp.apply(value, (v) => {
                this.meterData.low = v;
            });
        }
    }
    watchMax(value) {
        valueNumberClamped.maxProp.apply(value, (v) => {
            this.setRenderProp('max', v);
            this.watchValue(this.getRawProp('value'));
        });
    }
    watchMin(value) {
        minProp.apply(value, (v) => {
            this.setRenderProp('min', v);
            this.watchValue(this.getRawProp('value'));
        });
    }
    watchOptimum(value) {
        if (value === undefined) {
            this.meterData.optimum = undefined;
        }
        else {
            optimumProp.apply(value, (v) => {
                this.meterData.optimum = v;
            });
        }
    }
    watchOrientation(value) {
        orientationProp.apply(value, (v) => {
            this.setRenderProp('orientation', v);
        });
    }
    watchUnit(value) {
        valueNumberClamped.unitProp.apply(value, (v) => {
            this.setRenderProp('unit', v);
        });
    }
    watchValue(value) {
        this.setRawProp('value', value);
        valueNumberClamped.clampedNumberValueProp.apply(value, (v) => {
            this.setRenderProp('value', v);
        }, { min: this.getRenderProp('min'), max: this.getRenderProp('max') });
    }
}

const defaultStyleCss = "/* forward the rem function */\n/*\n* This file defines the layer order for all CSS layers used in KoliBri.\n* The order is important as it determines the cascade priority.\n*\n* Layer order (lowest to highest priority):\n* 1. kol-a11y - Accessibility defaults and requirements\n* 2. kol-global - Global component styles and resets\n* 3. kol-component - Component-specific styles\n* 4. kol-theme-global - Theme-specific global styles\n* 5. kol-theme-component - Theme-specific component styles\n*/\n@layer kol-a11y, kol-global, kol-component, kol-theme-global, kol-theme-component;\n/*\n * This file contains all rules for accessibility.\n */\n@layer kol-a11y {\n  :host {\n    /*\n                   * Minimum size of interactive elements.\n                   */\n    --a11y-min-size: calc(44 * 1rem / var(--kolibri-root-font-size, 16));\n    /*\n     * No element should be used without verifying the contrast ratio of its background and font colors.\n     * By initially setting the background color to white and the font color to black,\n     * the contrast ratio is ensured and explicit adjustment is forced.\n     */\n    color: black;\n    background-color: white;\n    /*\n     * Verdana is an accessible font that can be used without requiring additional loading time.\n     */\n    font-family: Verdana;\n    /*\n     * Letter spacing is required for all texts.\n     */\n    letter-spacing: inherit;\n    /*\n     * Word spacing is required for all texts.\n     */\n    word-spacing: inherit;\n    /*\n     * Text should be aligned left by default to provide a predictable starting point.\n     */\n    text-align: left;\n  }\n  * {\n    /*\n     * This rule enables the word dividing for all texts. That is important for high zoom levels.\n     */\n    hyphens: auto;\n    /*\n     * This rule enables the word dividing for all texts. That is important for high zoom levels.\n     */\n    word-break: break-word;\n  }\n  /*\n   * All interactive elements should have a minimum size of to-rem(44).\n   */\n  /* input:not([type='checkbox'], [type='radio'], [type='range']), */\n  /* option, */\n  /* select, */\n  /* textarea, */\n  button,\n  .kol-input .input {\n    min-width: var(--a11y-min-size);\n    min-height: var(--a11y-min-size);\n  }\n  /*\n   * Some interactive elements should not inherit the font-family and font-size.\n   */\n  a,\n  button,\n  h1,\n  h2,\n  h3,\n  h4,\n  h5,\n  h6,\n  input,\n  option,\n  select,\n  textarea {\n    /*\n     * All elements should inherit the text color from his parent element.\n     */\n    color: inherit;\n    /*\n     * All elements should inherit the font family from his parent element.\n     */\n    font-family: inherit;\n    /*\n     * All elements should inherit the font size from his parent element.\n     */\n    font-size: inherit;\n    /*\n     * Letter spacing is required for all texts.\n     */\n    letter-spacing: inherit;\n    /*\n     * Word spacing is required for all texts.\n     */\n    word-spacing: inherit;\n  }\n  /**\n  * Sometimes we need the semantic element for accessibility reasons,\n  * but we don't want to show it.\n  *\n  * - https://www.a11yproject.com/posts/how-to-hide-content/\n  */\n  .visually-hidden {\n    position: fixed;\n    top: 0;\n    left: 0;\n    width: 1px;\n    height: 1px;\n    overflow: hidden;\n    white-space: nowrap;\n    clip-path: inset(50%);\n  }\n}\n@layer kol-global {\n  /*\n   * Dieses CSS stellt sicher, dass der Standard-Style\n   * von A und Button resettet werden.\n   */\n  :is(a, button) {\n    background-color: transparent;\n    width: 100%;\n    margin: 0;\n    padding: 0;\n    border: none;\n    /* 100% needed for custom width from outside */\n  }\n  /*\n   * Ensure elements with hidden attribute to be actually not visible\n   * @see https://meowni.ca/hidden.is.a.lie.html\n   */\n  [hidden] {\n    display: none !important;\n  }\n  .badge-text-hint {\n    color: black;\n    background-color: white;\n  }\n}\n@layer kol-global {\n  :host {\n    /*\n     * The max-width is needed to prevent the table from overflowing the\n     * parent node, if the table is wider than the parent node.\n     */\n    max-width: 100%;\n    font-size: calc(16 * 1rem / var(--kolibri-root-font-size, 16));\n  }\n  * {\n    /*\n     * We prefer to box-sizing: border-box for all elements.\n     */\n    box-sizing: border-box;\n  }\n  .kol-span {\n    /* KolSpan is a layout component with icons in all directions and a label text in the middle. */\n    display: flex;\n    flex-flow: column;\n    align-items: center;\n    justify-content: center;\n    /* The sub span in KolSpan is the horizontal span with icon left and right and the label text in the middle. */\n  }\n  .kol-span__container {\n    display: flex;\n    align-items: center;\n  }\n  a,\n  button {\n    cursor: pointer;\n  }\n  .kol-span .kol-span__label--hide-label .kol-span__label {\n    display: none;\n  }\n  /* Reset browser agent style. */\n  button:disabled {\n    color: unset;\n  }\n  .disabled label,\n  .disabled:focus-within label,\n  [aria-disabled=true],\n  [aria-disabled=true]:focus,\n  [disabled],\n  [disabled]:focus {\n    outline: none;\n    cursor: not-allowed;\n  }\n  [aria-disabled=true]:focus .kol-span,\n  [disabled]:focus .kol-span {\n    outline: none !important;\n  }\n  .hastooltip {\n    z-index: 900 !important;\n  }\n}\n@layer kol-component {\n  .kol-meter {\n    --color-optimal: var(--kol-meter-color-optimal, #2ea32e);\n    --color-suboptimal: var(--kol-meter-color-suboptimal, #c8a000);\n    --color-critical: var(--kol-meter-color-critical, #c00);\n    --vertical-height: var(--kol-meter-vertical-height, 128);\n    --horizontal-height: var(--kol-meter-horizontal-height, 12);\n  }\n  .kol-meter__bar {\n    display: grid;\n    align-items: center;\n    grid-template-areas: \"label label\" \"bar value\";\n    grid-template-columns: 1fr auto;\n  }\n  .kol-meter__bar-label {\n    grid-column-end: 2;\n    grid-area: label;\n  }\n  .kol-meter__bar-track {\n    display: flex;\n    min-width: 100%;\n    grid-area: bar;\n  }\n  .kol-meter__value-unit {\n    grid-area: value;\n  }\n  .kol-meter:not(.kol-meter--vertical) .kol-meter__value {\n    display: inline-block;\n    text-align: right;\n  }\n  .kol-meter--vertical {\n    min-height: 100%;\n  }\n  .kol-meter--vertical .kol-meter__bar {\n    min-height: 100%;\n    align-items: end;\n    justify-items: center;\n    grid-template-areas: \"label\" \"bar\" \"value\";\n    grid-template-columns: 1fr;\n    grid-template-rows: min-content 1fr min-content;\n  }\n  .kol-meter--vertical .kol-meter__bar-label {\n    text-align: center;\n  }\n  .kol-meter--vertical .kol-meter__bar-track {\n    position: relative;\n    min-width: 0;\n    min-height: calc(var(--vertical-height) * 1rem / var(--kolibri-root-font-size, 16));\n    align-items: center;\n  }\n  .kol-meter--vertical .kol-meter__bar meter {\n    transform: rotate(-90deg);\n    transform-origin: left bottom;\n    position: absolute;\n    bottom: 0;\n    width: calc(var(--vertical-height) * 1rem / var(--kolibri-root-font-size, 16));\n  }\n  .kol-meter {\n    /* Base meter styling */\n  }\n  .kol-meter meter {\n    width: 100%;\n    height: calc(var(--horizontal-height) * 1rem / var(--kolibri-root-font-size, 16));\n    /* Remove default appearance in some browsers */\n    -webkit-appearance: none;\n    -moz-appearance: none;\n    appearance: none;\n  }\n  .kol-meter {\n    /* Chrome, Safari, Edge (WebKit/Blink) */\n  }\n  .kol-meter meter::-webkit-meter-bar {\n    background: white;\n    height: calc(var(--horizontal-height) * 1rem / var(--kolibri-root-font-size, 16));\n    border: 1px solid black;\n  }\n  .kol-meter meter::-webkit-meter-optimum-value {\n    background: var(--color-optimal);\n  }\n  .kol-meter meter::-webkit-meter-suboptimum-value {\n    background: var(--color-suboptimal);\n  }\n  .kol-meter meter::-webkit-meter-even-less-good-value {\n    background: var(--color-critical);\n  }\n  .kol-meter {\n    /* Firefox */\n  }\n  @supports (selector(::-moz-meter-bar)) {\n    .kol-meter meter {\n      background: white;\n      border: 1px solid black;\n    }\n  }\n  .kol-meter meter::-moz-meter-bar {\n    background: var(--color-optimal);\n  }\n  .kol-meter meter:-moz-meter-sub-optimum::-moz-meter-bar {\n    background: var(--color-suboptimal);\n  }\n  .kol-meter meter:-moz-meter-sub-sub-optimum::-moz-meter-bar {\n    background: var(--color-critical);\n  }\n}";

const KolMeter = class extends variantQuote.BaseWebComponent {
    constructor(hostRef) {
        super();
        index.registerInstance(this, hostRef);
        this.ctrl = new MeterController(this.stateAccess);
        this._max = 1;
        this._min = 0;
        this._orientation = 'horizontal';
        this._unit = '%';
    }
    watchHigh(value) {
        this.ctrl.watchHigh(value);
    }
    watchLabel(value) {
        this.ctrl.watchLabel(value);
    }
    watchLow(value) {
        this.ctrl.watchLow(value);
    }
    watchMax(value) {
        this.ctrl.watchMax(value);
    }
    watchMin(value) {
        this.ctrl.watchMin(value);
    }
    watchOptimum(value) {
        this.ctrl.watchOptimum(value);
    }
    watchOrientation(value) {
        this.ctrl.watchOrientation(value);
    }
    watchUnit(value) {
        this.ctrl.watchUnit(value);
    }
    watchValue(value) {
        this.ctrl.watchValue(value);
    }
    componentWillLoad() {
        this.ctrl.componentWillLoad({
            high: this._high,
            label: this._label,
            low: this._low,
            max: this._max,
            min: this._min,
            optimum: this._optimum,
            orientation: this._orientation,
            unit: this._unit,
            value: this._value,
        });
    }
    render() {
        const { high, low, optimum } = this.ctrl.getMeterData();
        return (index.h(index.Host, { key: '69ec1520b0cf6db98a038e285bc4cddea86c4531' }, index.h(MeterFC, { key: '2d8fadf97a3734760c4384e5b68c7bd059d299c3', high: high, label: this.ctrl.getRenderProp('label'), low: low, max: this.ctrl.getRenderProp('max'), min: this.ctrl.getRenderProp('min'), optimum: optimum, orientation: this.ctrl.getRenderProp('orientation'), unit: this.ctrl.getRenderProp('unit'), value: this.ctrl.getRenderProp('value') })));
    }
    static get watchers() { return {
        "_high": ["watchHigh"],
        "_label": ["watchLabel"],
        "_low": ["watchLow"],
        "_max": ["watchMax"],
        "_min": ["watchMin"],
        "_optimum": ["watchOptimum"],
        "_orientation": ["watchOrientation"],
        "_unit": ["watchUnit"],
        "_value": ["watchValue"]
    }; }
};
KolMeter.style = {
    default: defaultStyleCss
};

exports.kol_meter = KolMeter;
//# sourceMappingURL=kol-meter.entry.cjs.js.map
