import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";

/* eslint-disable */
import React, { useState } from 'react';
import { set } from 'dot-prop-immutable';
import { i18n } from '@fishx/i18n';
import { SimpleForm as Form, Switch, Select } from '@whalecloud/fdx';
import CustomInput from "../../../components/CustomInput";
import { toolFormAttr, transformDependencyValues, isSingleBoolen } from "../../../utils";
/**
 * cascadeSet
 * 设置表单级联属性
 */

var valueKey = 'data.formSettings.dependency';

var DisableRelation = function DisableRelation(props) {
  var updateElement = props.updateElement,
      element = props.element,
      relateOptions = props.relateOptions,
      formItems = props.formItems;
  var _element$data$formSet = element.data.formSettings;
  _element$data$formSet = _element$data$formSet === void 0 ? {} : _element$data$formSet;
  var _element$data$formSet2 = _element$data$formSet.dependency,
      dependency = _element$data$formSet2 === void 0 ? {} : _element$data$formSet2;
  var _dependency$disabled = dependency.disabled,
      disabled = _dependency$disabled === void 0 ? {} : _dependency$disabled;

  var _Form$useForm = Form.useForm(),
      _Form$useForm2 = _slicedToArray(_Form$useForm, 1),
      formDisable = _Form$useForm2[0];

  var _useState = useState(isSingleBoolen(disabled.inputs) ? 'switch' : ''),
      _useState2 = _slicedToArray(_useState, 2),
      relationTag = _useState2[0],
      setRelationTag = _useState2[1];

  var initialValues_disable = _objectSpread(_objectSpread({
    output: true,
    defaultOutput: false
  }, disabled), {}, {
    relates: disabled.relates ? disabled.relates.join(',') : '',
    inputs: disabled.inputs ? disabled.inputs.join(',') : ''
  });

  var onRelationChange = function onRelationChange(value) {
    var item = formItems.find(function (v) {
      return v.name === value;
    });

    if (item && item.type === 'switch') {
      setRelationTag('switch');
    } else {
      setRelationTag(''); //复原 relationTag
    }
  };

  var onDisableChange = function onDisableChange(changedValues, allValues) {
    var values = transformDependencyValues(allValues);
    var propName = "".concat(valueKey, ".disabled");
    var newElement = values ? set(element, propName, values) : set(element, valueKey, {});
    updateElement({
      element: newElement
    });
  };

  return /*#__PURE__*/React.createElement(Form, Object.assign({}, toolFormAttr, {
    form: formDisable,
    initialValues: initialValues_disable,
    onValuesChange: onDisableChange
  }), /*#__PURE__*/React.createElement(Form.Item, {
    label: i18n('PAGEBUIDER_SETTINGS_RELATION'),
    name: "relates"
  }, /*#__PURE__*/React.createElement(Select, {
    placeholder: i18n('PAGEBUIDER_SETTINGS_PLEASE_INPUT_VALUE'),
    allowClear: true,
    onChange: onRelationChange
  }, relateOptions.map(function (v) {
    return /*#__PURE__*/React.createElement(Select.Option, {
      value: v.value,
      title: v.title
    }, v.label);
  }))), relationTag === 'switch' ? /*#__PURE__*/React.createElement(Form.Item, {
    label: i18n('PAGEBUIDER_SETTINGS_INPUT'),
    name: "inputs",
    valuePropName: "checked"
  }, /*#__PURE__*/React.createElement(Switch, null)) : /*#__PURE__*/React.createElement(Form.Item, {
    label: i18n('PAGEBUIDER_SETTINGS_INPUT'),
    name: "inputs"
  }, /*#__PURE__*/React.createElement(CustomInput, {
    inputProps: {
      placeholder: i18n('PAGEBUIDER_SETTINGS_PLEASE_INPUT_VALUE')
    }
  })), /*#__PURE__*/React.createElement(Form.Item, {
    label: i18n('PAGEBUIDER_SETTINGS_OUTPUT'),
    name: "output",
    valuePropName: "checked"
  }, /*#__PURE__*/React.createElement(Switch, null)), /*#__PURE__*/React.createElement(Form.Item, {
    label: i18n('PAGEBUIDER_SETTINGS_DEFAULT_OUTPUT'),
    name: "defaultOutput",
    valuePropName: "checked"
  }, /*#__PURE__*/React.createElement(Switch, null)));
};

export default DisableRelation;
//# sourceMappingURL=DisableRelation.js.map