"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _react = _interopRequireWildcard(require("react"));
var _Field = _interopRequireDefault(require("./Field"));
var _utils = require("../utils");
var _FuncWidget = _interopRequireDefault(require("./FuncWidget"));
var _reactUtils = require("../../utils/reactUtils");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2["default"])(o), (0, _possibleConstructorReturn2["default"])(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2["default"])(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
var FieldWrapper = exports["default"] = /*#__PURE__*/function (_Component) {
  function FieldWrapper(props) {
    var _this;
    (0, _classCallCheck2["default"])(this, FieldWrapper);
    _this = _callSuper(this, FieldWrapper, [props]);
    _this.renderField = function () {
      var _this$props = _this.props,
        config = _this$props.config,
        classname = _this$props.classname,
        selectedField = _this$props.selectedField,
        selectedFieldSrc = _this$props.selectedFieldSrc,
        selectedFieldType = _this$props.selectedFieldType,
        setField = _this$props.setField,
        setFuncValue = _this$props.setFuncValue,
        parentField = _this$props.parentField,
        readonly = _this$props.readonly,
        id = _this$props.id,
        groupId = _this$props.groupId,
        fieldError = _this$props.fieldError,
        valueError = _this$props.valueError;
      var supportedFieldSrcs = ["func", "field"];
      if (!supportedFieldSrcs.includes(selectedFieldSrc)) {
        return "?";
      }
      var field = selectedFieldSrc === "func" ? /*#__PURE__*/_react["default"].createElement(_FuncWidget["default"], {
        isLHS: true,
        config: config,
        value: selectedField,
        fieldSrc: selectedFieldSrc,
        fieldType: selectedFieldType,
        fieldError: fieldError,
        valueError: valueError,
        parentField: parentField,
        setValue: setField,
        setFuncValue: setFuncValue,
        readonly: readonly,
        id: id,
        groupId: groupId,
        key: "field-func-" + id
      }) : /*#__PURE__*/_react["default"].createElement(_Field["default"], {
        config: config,
        selectedField: selectedField,
        selectedFieldSrc: selectedFieldSrc,
        selectedFieldType: selectedFieldType,
        parentField: parentField,
        setField: setField,
        customProps: config.settings.customFieldSelectProps,
        readonly: readonly,
        id: id,
        groupId: groupId,
        key: "field-" + id
      });
      var fieldLabel = selectedFieldSrc === "func" ? config.settings.funcLabel : config.settings.fieldLabel;
      var label = config.settings.showLabels && selectedFieldSrc !== "func" && /*#__PURE__*/_react["default"].createElement("label", {
        className: "rule--label"
      }, fieldLabel);
      return /*#__PURE__*/_react["default"].createElement("div", {
        key: selectedFieldSrc,
        className: classname
      }, label, field);
    };
    _this.renderFieldSources = function () {
      if (!_this.meta) return null;
      var _this$props2 = _this.props,
        config = _this$props2.config,
        readonly = _this$props2.readonly,
        selectedFieldSrc = _this$props2.selectedFieldSrc,
        setFieldSrc = _this$props2.setFieldSrc,
        id = _this$props2.id;
      var fieldSourcesOptions = _this.meta.fieldSourcesOptions;
      var settings = config.settings;
      var ValueSources = _this.ValueSources;
      var sourceLabel = settings.showLabels ? /*#__PURE__*/_react["default"].createElement("label", {
        className: "rule--label"
      }, "\xA0") : null;
      return fieldSourcesOptions.length > 1 && !readonly && /*#__PURE__*/_react["default"].createElement("div", {
        key: "fieldsrc",
        className: "rule--fieldsrc"
      }, sourceLabel, /*#__PURE__*/_react["default"].createElement(ValueSources, {
        key: "fieldsrc-" + id,
        valueSources: fieldSourcesOptions,
        valueSrc: selectedFieldSrc,
        config: config,
        setValueSrc: setFieldSrc,
        readonly: readonly,
        title: settings.fieldSourcesPopupTitle
      }));
    };
    (0, _reactUtils.useOnPropsChanged)(_this);
    _this.onPropsChanged(props);
    return _this;
  }
  (0, _inherits2["default"])(FieldWrapper, _Component);
  return (0, _createClass2["default"])(FieldWrapper, [{
    key: "onPropsChanged",
    value: function onPropsChanged(nextProps) {
      var prevProps = this.props;
      var configChanged = !this.ValueSources || (prevProps === null || prevProps === void 0 ? void 0 : prevProps.config) !== (nextProps === null || nextProps === void 0 ? void 0 : nextProps.config);
      var keysForMeta = ["config"];
      var needUpdateMeta = !this.meta || keysForMeta.map(function (k) {
        return nextProps[k] !== prevProps[k];
      }).filter(function (ch) {
        return ch;
      }).length > 0;
      if (needUpdateMeta) {
        this.meta = this.getMeta(nextProps);
      }
      if (configChanged) {
        var config = nextProps.config;
        var renderValueSources = config.settings.renderValueSources;
        this.ValueSources = function (pr) {
          return renderValueSources(pr, config.ctx);
        };
      }
    }
  }, {
    key: "getMeta",
    value: function getMeta(_ref) {
      var config = _ref.config;
      var defaultValueSourcesLabels = {
        value: "Value",
        field: "Field",
        func: "Function"
      };
      var _config$settings = config.settings,
        valueSourcesInfo = _config$settings.valueSourcesInfo,
        fieldSources = _config$settings.fieldSources;
      var fieldSourcesOptions = fieldSources.map(function (srcKey) {
        var _ref2, _valueSourcesInfo$src, _valueSourcesInfo$src2;
        return [srcKey, {
          label: (_ref2 = (_valueSourcesInfo$src = (_valueSourcesInfo$src2 = valueSourcesInfo[srcKey]) === null || _valueSourcesInfo$src2 === void 0 ? void 0 : _valueSourcesInfo$src2.label) !== null && _valueSourcesInfo$src !== void 0 ? _valueSourcesInfo$src : defaultValueSourcesLabels[srcKey]) !== null && _ref2 !== void 0 ? _ref2 : srcKey
        }];
      });
      return {
        fieldSourcesOptions: fieldSourcesOptions
      };
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props3 = this.props,
        classname = _this$props3.classname,
        canSelectFieldSource = _this$props3.canSelectFieldSource;
      var fieldSources = canSelectFieldSource && this.renderFieldSources();
      var field = this.renderField();
      if (!canSelectFieldSource) {
        return field;
      } else {
        return /*#__PURE__*/_react["default"].createElement(_utils.Col, {
          className: "rule--field-wrapper"
        }, fieldSources, field);
      }
    }
  }]);
}(_react.Component);