"use strict";

exports.__esModule = true;
exports.default = void 0;
var _unidriver = require("../../unidriver");
var _unidriver2 = require("@wix/design-system/dist/testkit/unidriver");
var _dataHooks = require("../dataHooks");
var _DatePickerUni = require("../../unidriver/DatePicker.uni.driver");
var _default = (base, body) => {
  const fieldType = () => base.attr('data-field-type');
  const inputBase = (suffix = '') => base.$(`[data-hook="${_dataHooks.dataHooks.Input}${suffix}"]`);
  const dropdown = () => (0, _unidriver2.DropdownUniDriver)(inputBase(), body);
  const checkbox = () => (0, _unidriver2.CheckboxUniDriver)(inputBase(), body);
  const formField = () => (0, _unidriver2.FormFieldUniDriver)(base.$(`[data-hook="${_dataHooks.dataHooks.Label}"]`), body);
  return {
    /**
     * Gets field label
     */
    getLabel: async () => {
      const type = await fieldType();
      if (type === 'checkbox') {
        return checkbox().getLabel();
      }
      const element = await formField().getLabel();
      return element == null ? void 0 : element.textContent;
    },
    /**
     * Gets field value
     */
    getValue: async () => {
      const type = await fieldType();
      switch (type) {
        case 'url':
        case 'shortText':
          return (0, _unidriver2.InputUniDriver)(inputBase(), body).getValue();
        case 'date':
          return (await (0, _DatePickerUni.DatePickerUniDriver)(inputBase(), body)).inputDriver.getValue();
        case 'dateTime':
          const date = await (await (0, _DatePickerUni.DatePickerUniDriver)(inputBase('-date'), body)).inputDriver.getValue();
          const format = Intl.DateTimeFormat([], {
            hour: 'numeric',
            minute: 'numeric'
          });
          const time = await (0, _unidriver2.TimeInputUniDriver)(inputBase('-time'), body).getValue();
          if (isNaN(time.getTime())) {
            return date;
          }
          return `${date} ${format.format(time)}`;
        case 'checkbox':
          return checkbox().isChecked();
        case 'longText':
          return (0, _unidriver2.InputAreaUniDriver)(inputBase(), body).getValue();
        case 'dropdown':
          return dropdown().inputDriver.getValue();
        case 'integer':
        case 'decimal':
          return (0, _unidriver2.NumberInputUniDriver)(inputBase(), body).getValue();
        default:
          throw new Error(`Driver for type '${type}' not found.`);
      }
    },
    /**
     * Gets field status message
     */
    getStatusMessage: async () => {
      const type = await fieldType();
      switch (type) {
        case 'shortText':
        case 'url':
          return (0, _unidriver2.FormFieldUniDriver)(base, body).getStatusMessage();
        case 'date':
          return (await (0, _DatePickerUni.DatePickerUniDriver)(inputBase(), body)).inputDriver.getStatusMessage();
        case 'dateTime':
          let dateStatusMessage;
          let timeStatusMessage;
          try {
            dateStatusMessage = await (await (0, _DatePickerUni.DatePickerUniDriver)(inputBase('-date'), body)).inputDriver.getStatusMessage();
          } catch {}
          try {
            timeStatusMessage = await (0, _unidriver2.TimeInputUniDriver)(inputBase('-time'), body).getStatusMessage();
          } catch {}
          return [dateStatusMessage, timeStatusMessage].join('\n').trim();
        case 'longText':
          return (0, _unidriver2.InputAreaUniDriver)(inputBase(), body).getStatusMessage();
        case 'dropdown':
          return dropdown().inputDriver.getStatusMessage();
        case 'integer':
        case 'decimal':
          return (0, _unidriver2.NumberInputUniDriver)(inputBase(), body).getStatusMessage();
        default:
          throw new Error(`Driver for type '${type}' not found.`);
      }
    },
    /**
     * Enters field value, handles different value types based on field type
     */
    enterValue: async (value, {
      locale = 'en'
    } = {}) => {
      const type = await fieldType();
      switch (type) {
        case 'url':
          const inputDriver = (0, _unidriver2.InputUniDriver)(inputBase(), body);
          await inputDriver.enterText(value);
          const urlNative = await inputBase().getNative();
          // jsdom-only: enterText doesn't dispatch blur, so we do it manually.
          // Skip for sled-2 / sled-3 (real browsers handle blur natively).
          if (!(0, _unidriver.isSled2)(urlNative) && !(0, _unidriver.isSled3)(urlNative)) {
            await inputDriver.blur();
          }
          return;
        case 'shortText':
          return (0, _unidriver2.InputUniDriver)(inputBase(), body).enterText(value);
        case 'date':
          const datePickerUniDriver = await (0, _DatePickerUni.DatePickerUniDriver)(inputBase(), body);
          await datePickerUniDriver.inputDriver.enterText(typeof value === 'string' ? value : `${Intl.DateTimeFormat().format(value)}`);
          const dateNative = await inputBase().getNative();
          // jsdom-only blur (see url case above).
          if (!(0, _unidriver.isSled2)(dateNative) && !(0, _unidriver.isSled3)(dateNative)) {
            await datePickerUniDriver.inputDriver.blur();
          }
          return;
        case 'dateTime':
          let dateValue = '';
          let timeValue = '';
          if (typeof value === 'string') {
            const parts = value.split('T');
            dateValue = parts[0];
            timeValue = parts[1];
          } else {
            dateValue = `${Intl.DateTimeFormat([locale]).format(value)}`;
            timeValue = Intl.DateTimeFormat([locale], {
              hour: 'numeric',
              minute: 'numeric'
            }).format(value);
          }
          const datetimePickerUniDriver = await (0, _DatePickerUni.DatePickerUniDriver)(inputBase('-date'), body);
          await datetimePickerUniDriver.inputDriver.enterText(dateValue);
          const datetimeDateNative = await inputBase('-date').getNative();
          // jsdom-only: we do it twice because of a bug when removing the
          // value in the first time, then blur to commit. Real browsers
          // (sled-2 / sled-3) don't have either issue.
          if (!(0, _unidriver.isSled2)(datetimeDateNative) && !(0, _unidriver.isSled3)(datetimeDateNative)) {
            await datetimePickerUniDriver.inputDriver.enterText(dateValue);
            await datetimePickerUniDriver.inputDriver.blur();
          }
          const timeInput = (0, _unidriver2.TimeInputUniDriver)(inputBase('-time'), body);
          await timeInput.setValue(timeValue);
          const datetimeTimeNative = await inputBase('-time').getNative();
          if (!(0, _unidriver.isSled2)(datetimeTimeNative) && !(0, _unidriver.isSled3)(datetimeTimeNative)) {
            await timeInput.confirmValue();
          }
          return;
        case 'checkbox':
          const checkboxDriver = checkbox();
          const isChecked = await checkboxDriver.isChecked();
          if (value && !isChecked) {
            await checkboxDriver.click();
          } else if (isChecked && !value) {
            await checkboxDriver.click();
          }
          return;
        case 'longText':
          return (0, _unidriver2.InputAreaUniDriver)(inputBase(), body).enterText(value);
        case 'dropdown':
          const dropdownDriver = dropdown();
          await dropdownDriver.dropdownLayoutDriver.click();
          return dropdownDriver.dropdownLayoutDriver.clickAtOptionWithValue(value);
        case 'integer':
        case 'decimal':
          return (0, _unidriver2.NumberInputUniDriver)(inputBase(), body).enterText(value);
        default:
          throw new Error(`Driver for type '${type}' not found.`);
      }
    },
    /**
     * Returns available dropdown options
     */
    getOptions: async () => {
      const type = await fieldType();
      if (type !== 'dropdown') {
        throw new Error(`Field type '${type}' does not support this method.`);
      }
      return dropdown().dropdownLayoutDriver.optionsContent();
    },
    /**
     * Returns remaining chars count on fields with maxLength
     */
    getLengthLeft: async () => {
      return formField().getLengthLeft();
    },
    ...(0, _unidriver.baseUniDriverFactory)(base)
  };
};
exports.default = _default;
//# sourceMappingURL=Field.uni.driver.js.map