import _extends from "@babel/runtime/helpers/extends";
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["widgetFactory"],
  _excluded2 = ["factory"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import React, { useMemo, memo } from "react";
import { Utils } from "@react-awesome-query-builder/core";
import omit from "lodash/omit";
var getTitleInListValues = Utils.ListUtils.getTitleInListValues;
var _widgetDefKeysToOmit = Utils.ConfigUtils._widgetDefKeysToOmit;
var _Utils$TreeUtils = Utils.TreeUtils,
  _fixImmutableValue = _Utils$TreeUtils._fixImmutableValue,
  isImmutable = _Utils$TreeUtils.isImmutable;
var WidgetFactoryMemo = /*#__PURE__*/memo(function (_ref) {
  var widgetFactory = _ref.widgetFactory,
    widgetProps = _objectWithoutProperties(_ref, _excluded);
  var config = widgetProps.config,
    isFuncArg = widgetProps.isFuncArg,
    fieldDefinition = widgetProps.fieldDefinition;
  var _ref2 = fieldDefinition || {},
    fieldSettings = _ref2.fieldSettings,
    defaultValue = _ref2.defaultValue,
    valueSources = _ref2.valueSources;
  var isConst = isFuncArg && (valueSources === null || valueSources === void 0 ? void 0 : valueSources.length) == 1 && (valueSources === null || valueSources === void 0 ? void 0 : valueSources[0]) === "const";
  var listValues = (fieldSettings === null || fieldSettings === void 0 ? void 0 : fieldSettings.treeValues) || (fieldSettings === null || fieldSettings === void 0 ? void 0 : fieldSettings.listValues);
  if (isConst && defaultValue) {
    if (typeof defaultValue === "boolean") {
      return defaultValue ? widgetProps.labelYes || "YES" : widgetProps.labelNo || "NO";
    } else if (listValues) {
      if (Array.isArray(defaultValue)) return defaultValue.map(function (v) {
        return getTitleInListValues(listValues, v) || v;
      }).join(", ");else return getTitleInListValues(listValues, defaultValue) || defaultValue;
    }
    return "" + defaultValue;
  }
  if (!widgetFactory) {
    return "?";
  }
  return widgetFactory(widgetProps, config.ctx);
});
WidgetFactoryMemo.displayName = "WidgetFactoryMemo";
var WidgetFactory = function WidgetFactory(_ref3) {
  var delta = _ref3.delta,
    isFuncArg = _ref3.isFuncArg,
    valueSrc = _ref3.valueSrc,
    immValue = _ref3.value,
    immValueError = _ref3.valueError,
    fieldError = _ref3.fieldError,
    asyncListValues = _ref3.asyncListValues,
    isSpecialRange = _ref3.isSpecialRange,
    fieldDefinition = _ref3.fieldDefinition,
    widget = _ref3.widget,
    widgetDefinition = _ref3.widgetDefinition,
    widgetValueLabel = _ref3.widgetValueLabel,
    valueLabels = _ref3.valueLabels,
    textSeparators = _ref3.textSeparators,
    setValue = _ref3.setValue,
    setFuncValue = _ref3.setFuncValue,
    config = _ref3.config,
    field = _ref3.field,
    fieldSrc = _ref3.fieldSrc,
    fieldPath = _ref3.fieldPath,
    fieldType = _ref3.fieldType,
    isLHS = _ref3.isLHS,
    operator = _ref3.operator,
    readonly = _ref3.readonly,
    parentField = _ref3.parentField,
    parentFuncs = _ref3.parentFuncs,
    id = _ref3.id,
    groupId = _ref3.groupId,
    widgetId = _ref3.widgetId;
  var widgetFactory = widgetDefinition.factory,
    fieldWidgetProps = _objectWithoutProperties(widgetDefinition, _excluded2);

  // Widget value (if it's not a func with args) should NOT be Immutable
  // Eg. for multiselect value should be a JS Array, not Immutable List
  var fixedImmValue = immValue ? immValue.map(function (v) {
    return _fixImmutableValue(v);
  }) : undefined;
  var value = useMemo(function () {
    if (isSpecialRange) {
      var _value = [fixedImmValue === null || fixedImmValue === void 0 ? void 0 : fixedImmValue.get(0), fixedImmValue === null || fixedImmValue === void 0 ? void 0 : fixedImmValue.get(1)];
      if (_value[0] === undefined && _value[1] === undefined) _value = undefined;
      return _value;
    }
    return fixedImmValue === null || fixedImmValue === void 0 ? void 0 : fixedImmValue.get(delta);
  }, [isSpecialRange, immValue]);
  var valueError = useMemo(function () {
    if (!immValueError) {
      return null;
    }
    if (isSpecialRange) {
      return [immValueError.get(0), immValueError.get(1), immValueError.get(2)];
    }
    return immValueError === null || immValueError === void 0 ? void 0 : immValueError.get(delta);
  }, [isSpecialRange, immValue]);
  var errorMessage = isLHS ? fieldError : valueError;
  var _ref4 = fieldDefinition || {},
    fieldSettings = _ref4.fieldSettings;
  var label = widgetValueLabel.label,
    placeholder = widgetValueLabel.placeholder;
  var placeholders = valueLabels ? valueLabels.placeholder : null;
  var simpleField = isImmutable(field) ? null : field;
  var widgetProps = omit(_objectSpread(_objectSpread(_objectSpread({}, fieldWidgetProps), fieldSettings), {}, {
    config: config,
    field: simpleField,
    // !!! using of field prop in widget is strongly discouraged
    fieldPath: fieldPath,
    fieldSrc: fieldSrc,
    fieldType: fieldType,
    isLHS: isLHS,
    parentField: parentField,
    parentFuncs: parentFuncs,
    fieldDefinition: fieldDefinition,
    operator: operator,
    delta: delta,
    isSpecialRange: isSpecialRange,
    isFuncArg: isFuncArg,
    value: value,
    valueSrc: valueSrc,
    valueError: valueError,
    fieldError: fieldError,
    errorMessage: errorMessage,
    label: label,
    placeholder: placeholder,
    placeholders: placeholders,
    textSeparators: textSeparators,
    setValue: setValue,
    setFuncValue: setFuncValue,
    readonly: readonly,
    asyncListValues: asyncListValues,
    id: id,
    groupId: groupId,
    widgetId: widgetId,
    widgetFactory: widgetFactory,
    widget: widget
  }), [].concat(_toConsumableArray(_widgetDefKeysToOmit), ["toJS"]));
  return /*#__PURE__*/React.createElement(WidgetFactoryMemo, _extends({
    widgetFactory: widgetFactory
  }, widgetProps));
};
WidgetFactory.displayName = "WidgetFactory";
export default WidgetFactory;