import { __decorate } from 'tslib';
import { html, nothing } from 'lit';
import { property, state } from 'lit/decorators.js';
import { classMap } from 'lit/directives/class-map.js';
import { ifDefined } from 'lit/directives/if-defined.js';
import { live } from 'lit/directives/live.js';
import FormControlElement from '../../base/form-control-element.js';
import { defaultValue } from '../../utils/defaultvalue.js';
import { SgdsFormValidatorMixin } from '../../utils/validatorMixin.js';
import { watch } from '../../utils/watch.js';
import css_248z$1 from './checkbox.js';
import css_248z from '../../styles/form-check.js';

/**
 * @summary Checkbox component is used when you require users to select multiple items from a list.
 *
 * @slot default - The label of checkbox.
 *
 * @event sgds-change - Emitted when the checked state changes.
 * @event sgds-blur - Emitted when input is not in focus.
 * @event sgds-focus - Emitted when input is in focus.
 * @event sgds-check - Emitted when checkbox is checked
 * @event sgds-uncheck - Emitted when checkbox is unchecked
 */
class SgdsCheckbox extends SgdsFormValidatorMixin(FormControlElement) {
    constructor() {
        super(...arguments);
        /** Draws the checkbox in a checked state. */
        this.checked = false;
        /** Gets or sets the default value used to reset this element. The initial value corresponds to the one originally specified in the HTML that created this element. */
        this.defaultChecked = false;
        /** Marks the checkbox input as indeterminate , with indeterminate logo  */
        this.indeterminate = false;
        /** Makes the checkbox a required field. */
        this.required = false;
        /**Feedback text for error state when validated */
        this.invalidFeedback = "";
        this._isTouched = false;
    }
    /** Simulates a click on the checkbox. */
    click() {
        this.input.click();
    }
    /** Sets focus on the checkbox. */
    focus(options) {
        this.input.focus(options);
    }
    /** Removes focus from the checkbox. */
    blur() {
        this.input.blur();
    }
    _handleChange(e) {
        if (this.indeterminate) {
            this.indeterminate = !this.indeterminate;
        }
        this.checked = !this.checked;
        super._mixinHandleChange(e);
        this.emit("sgds-change", { detail: { checked: this.checked, value: this.value } });
        this.checked
            ? this.emit("sgds-check", { detail: { value: this.value } })
            : this.emit("sgds-uncheck", { detail: { value: this.value } });
    }
    _handleKeyDown(event) {
        const hasModifier = event.metaKey || event.ctrlKey || event.shiftKey || event.altKey;
        if (event.key === "Enter" && !hasModifier) {
            this.click();
        }
    }
    _handleBlur() {
        this._isTouched = true;
        this.emit("sgds-blur");
    }
    _handleFocus() {
        this.emit("sgds-focus");
    }
    _handleInvalid(e) {
        e.preventDefault();
        this.invalid = true;
    }
    /** @internal */
    _handleDisabledChange() {
        // Disabled form controls are always valid, so we need to recheck validity when the state changes
        this.setInvalid(false);
    }
    _handleIsTouched() {
        if (this._isTouched) {
            this.invalid = !this.input.checkValidity();
        }
    }
    _mixinResetFormControl() {
        this._isTouched = false;
        this.checked = this.input.checked = this.defaultChecked;
        this.input.dispatchEvent(new InputEvent("reset"));
        this._mixinResetValidity(this.input);
    }
    /**
     * Checks for validity. Under the hood, HTMLFormElement's reportValidity method calls this method to check for component's validity state
     * Note that the native error popup is prevented for SGDS form components by default. Instead the validation message shows up in the feedback container of SgdsInput
     */
    reportValidity() {
        return this._mixinReportValidity();
    }
    /**
     * Checks for validity without any native error popup message
     */
    checkValidity() {
        return this._mixinCheckValidity();
    }
    /**
     * Returns the ValidityState object
     */
    get validity() {
        return this._mixinGetValidity();
    }
    /**
     * Returns the validation message based on the ValidityState
     */
    get validationMessage() {
        return this._mixinGetValidationMessage();
    }
    firstUpdated(_changedProperties) {
        super.firstUpdated(_changedProperties);
        this.checked && this.emit("sgds-check", { detail: { value: this.value } });
    }
    render() {
        const displayFeedbackStyle = this.hasFeedback === "both" || this.hasFeedback === "style";
        const displayFeedbackText = this.hasFeedback === "both" || this.hasFeedback === "text";
        return html `
      <div class="form-check">
        <input
          class=${classMap({
            "form-check-input": true,
            "is-invalid": displayFeedbackStyle && this.invalid
        })}
          type="checkbox"
          id=${this._controlId}
          aria-invalid=${this.invalid ? "true" : "false"}
          name=${ifDefined(this.name)}
          ?indeterminate=${this.indeterminate}
          ?required=${this.required}
          aria-disabled=${this.disabled ? "true" : "false"}
          aria-checked=${this.checked ? "true" : "false"}
          @change=${(e) => this._handleChange(e)}
          @keydown=${this._handleKeyDown}
          @invalid=${(e) => this._handleInvalid(e)}
          ?checked=${live(this.checked)}
          .disabled=${this.disabled}
          .required=${this.required}
          @blur=${this._handleBlur}
          @focus=${this._handleFocus}
        />
        <label for="${this._controlId}" class="form-check-label" id="${this._labelId}"><slot></slot></label>
      </div>
      ${displayFeedbackText && this.invalid
            ? html `
            <div class="invalid-feedback-container">
              <slot name="invalidIcon">
                <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 20 20" fill="none">
                  <path
                    d="M17.5 10C17.5 14.1421 14.1421 17.5 10 17.5C5.85786 17.5 2.5 14.1421 2.5 10C2.5 5.85786 5.85786 2.5 10 2.5C14.1421 2.5 17.5 5.85786 17.5 10ZM10 6.25C9.49805 6.25 9.10584 6.68339 9.15578 7.18285L9.48461 10.4711C9.51109 10.7359 9.7339 10.9375 10 10.9375C10.2661 10.9375 10.4889 10.7359 10.5154 10.4711L10.8442 7.18285C10.8942 6.68339 10.5019 6.25 10 6.25ZM10.0014 11.875C9.48368 11.875 9.06394 12.2947 9.06394 12.8125C9.06394 13.3303 9.48368 13.75 10.0014 13.75C10.5192 13.75 10.9389 13.3303 10.9389 12.8125C10.9389 12.2947 10.5192 11.875 10.0014 11.875Z"
                    fill="currentColor"
                  />
                </svg>
              </slot>
              <div id="checkbox-feedback" tabindex="0" class="invalid-feedback">
                ${this.invalidFeedback ? this.invalidFeedback : this.input.validationMessage}
              </div>
            </div>
          `
            : nothing}
    `;
    }
}
SgdsCheckbox.styles = [...FormControlElement.styles, css_248z, css_248z$1];
__decorate([
    property({ type: String, reflect: true })
], SgdsCheckbox.prototype, "value", void 0);
__decorate([
    property({ type: Boolean, reflect: true })
], SgdsCheckbox.prototype, "checked", void 0);
__decorate([
    property({ type: String, reflect: true })
], SgdsCheckbox.prototype, "hasFeedback", void 0);
__decorate([
    defaultValue("checked")
], SgdsCheckbox.prototype, "defaultChecked", void 0);
__decorate([
    property({ type: Boolean, reflect: true })
], SgdsCheckbox.prototype, "indeterminate", void 0);
__decorate([
    property({ type: Boolean, reflect: true })
], SgdsCheckbox.prototype, "required", void 0);
__decorate([
    property({ type: String, reflect: true })
], SgdsCheckbox.prototype, "invalidFeedback", void 0);
__decorate([
    state()
], SgdsCheckbox.prototype, "_isTouched", void 0);
__decorate([
    watch("disabled", { waitUntilFirstUpdate: true })
], SgdsCheckbox.prototype, "_handleDisabledChange", null);
__decorate([
    watch("_isTouched", { waitUntilFirstUpdate: true })
], SgdsCheckbox.prototype, "_handleIsTouched", null);

export { SgdsCheckbox, SgdsCheckbox as default };
//# sourceMappingURL=sgds-checkbox.js.map
