import { MaskClipboardContext, MaskFocusContext, MaskKeyboardContext, MaskLocaleContext, MaskMouseContext, MaskValueChangedContext, } from "../context";
export class MaskComponentAdapter {
  constructor(mask) {
    this.mask = mask;
  }
  get attributes() {
    return {
      inputMode: this.mask.inputMode,
      maxLength: this.mask.maxLength,
      minLength: this.mask.minLength,
      type: 'text',
      autoCapitalize: 'off',
      autoCorrect: 'off',
      spellcheck: false,
    };
  }
  bindComponent(component) {
    this.component = component;
  }
  bindComponentDidLoad() {
    if (this.component && this.component.nativeInput) {
      this.component.nativeInput.value = this.component.value || '';
    }
    this.bindValueChanged(this.component.value, undefined);
  }
  bindConfigChanged(config) {
    const { component, mask } = this;
    const context = new MaskLocaleContext({
      component,
      mask,
      event: {
        locale: `${config.language}-${config.region}`,
      },
    });
    this.mask.fireI18nChange(context);
    context.submit();
  }
  bindValueChanged(newValue, oldValue) {
    const { component, mask } = this;
    const context = new MaskValueChangedContext({
      component,
      mask,
      event: { newValue, oldValue },
    });
    this.mask.fireValueChanged(context);
    context.submit('blur');
  }
  bindKeyDown(event) {
    if (this.isComponentAccessible) {
      const { component, mask } = this;
      const context = new MaskKeyboardContext({ event, component, mask });
      this.mask.fireKeyDown(context);
      const isTabKey = event.key === 'Tab';
      context.submit(isTabKey ? 'tab' : 'input');
      component.balKeyPress.emit(event);
    }
  }
  bindFocus(event) {
    if (this.isComponentAccessible) {
      const { component, mask } = this;
      component.focused = true;
      const context = new MaskFocusContext({ event, component, mask });
      this.mask.fireFocus(context);
      context.submit();
      component.balFocus.emit(event);
    }
  }
  bindBlur(event) {
    if (this.isComponentAccessible) {
      const { component, mask } = this;
      component.focused = false;
      const context = new MaskFocusContext({ event, component, mask });
      this.mask.fireBlur(context);
      context.submit('blur');
      component.balBlur.emit(event);
    }
  }
  bindPaste(event) {
    if (this.isComponentAccessible) {
      const { component, mask } = this;
      const context = new MaskClipboardContext({ event, component, mask });
      this.mask.firePaste(context);
      context.submit();
    }
  }
  bindFormReset(event) {
    const formElement = event.target;
    if (formElement && formElement.contains(this.component.el)) {
      this.component.value = this.component.initialValue;
      this.component.inputValue = this.component.initialValue;
      clearTimeout(this.resetHandlerTimer);
      this.resetHandlerTimer = setTimeout(() => {
        if (this.component.nativeInput) {
          this.component.nativeInput.value = this.component.value || '';
        }
      });
    }
  }
  bindClick(event) {
    if (this.isComponentAccessible) {
      const { component, mask } = this;
      const context = new MaskMouseContext({ event, component, mask });
      this.mask.fireClick(context);
      context.submit();
    }
  }
  bindHostClick(event) {
    if (!this.isComponentAccessible) {
      event.preventDefault();
      event.stopPropagation();
    }
  }
  bindGlobalClick(event) {
    if (!this.isComponentAccessible) {
      if (event.target && event.target === this.component.el) {
        event.preventDefault();
        event.stopPropagation();
      }
    }
  }
  get isComponentAccessible() {
    return !this.component.disabled && !this.component.readonly;
  }
}
