'use strict';

var _extends = require('@babel/runtime/helpers/extends');
var React = require('react');
var PropTypes = require('prop-types');
var react = require('@carbon/react');
var Settings = require('../../../constants/Settings.js');
var SharedPropTypes = require('../../../constants/SharedPropTypes.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var _extends__default = /*#__PURE__*/_interopDefault(_extends);
var React__default = /*#__PURE__*/_interopDefault(React);
var PropTypes__default = /*#__PURE__*/_interopDefault(PropTypes);

var prefix = Settings.settings.prefix,
  iotPrefix = Settings.settings.iotPrefix;
var propTypes = {
  /** Disables the form elements when true */
  disabled: PropTypes__default.default.bool,
  /** The state values of the controlled form elements e.g. { title: 'My view 1', isDefault: true } */
  formValues: PropTypes__default.default.objectOf(PropTypes__default.default.oneOfType([PropTypes__default.default.string, PropTypes__default.default.number, PropTypes__default.default.bool])).isRequired,
  /** Internationalisation strings */
  i18n: PropTypes__default.default.shape({
    titleInputLabelText: PropTypes__default.default.string,
    defaultCheckboxLabelText: PropTypes__default.default.string,
    publicCheckboxLabelText: PropTypes__default.default.string
  }).isRequired,
  /** Callback for when any of the form element's value changes */
  onChange: PropTypes__default.default.func.isRequired,
  /** Used to overide the internal components and props for advanced customisation */
  overrides: PropTypes__default.default.shape({
    form: SharedPropTypes.OverridePropTypes,
    titleTextInput: SharedPropTypes.OverridePropTypes,
    viewDescriptionContainer: SharedPropTypes.OverridePropTypes,
    defaultCheckbox: SharedPropTypes.OverridePropTypes,
    publicCheckbox: SharedPropTypes.OverridePropTypes
  }),
  /** The id of the form elememt that should be initially focused */
  primaryInputId: PropTypes__default.default.string,
  /** Id that can be used for testing */
  testID: PropTypes__default.default.string,
  /** When true it will show that the title field has invalid input */
  titleInputInvalid: PropTypes__default.default.bool,
  /** Used to describe the input validation error for the the title field */
  titleInputInvalidText: PropTypes__default.default.string,
  /** A text that describes what this view contains */
  viewDescription: PropTypes__default.default.string
};
var defaultProps = {
  disabled: false,
  overrides: undefined,
  primaryInputId: undefined,
  testID: 'TableSaveViewForm',
  titleInputInvalid: false,
  titleInputInvalidText: undefined,
  viewDescription: undefined
};
var preventFormSubmission = function preventFormSubmission(e) {
  return e.preventDefault();
};
var TableSaveViewForm = function TableSaveViewForm(_ref) {
  var _overrides$form, _overrides$titleTextI, _overrides$viewDescri, _overrides$defaultChe, _overrides$publicChec, _overrides$form2, _overrides$titleTextI2, _overrides$viewDescri2, _overrides$defaultChe2, _overrides$publicChec2;
  var formValues = _ref.formValues,
    disabled = _ref.disabled,
    _ref$i18n = _ref.i18n,
    titleInputLabelText = _ref$i18n.titleInputLabelText,
    defaultCheckboxLabelText = _ref$i18n.defaultCheckboxLabelText,
    publicCheckboxLabelText = _ref$i18n.publicCheckboxLabelText,
    _onChange = _ref.onChange,
    overrides = _ref.overrides,
    primaryInputId = _ref.primaryInputId,
    testID = _ref.testID,
    titleInputInvalid = _ref.titleInputInvalid,
    titleInputInvalidText = _ref.titleInputInvalidText,
    viewDescription = _ref.viewDescription;
  var MyForm = (overrides === null || overrides === void 0 || (_overrides$form = overrides.form) === null || _overrides$form === void 0 ? void 0 : _overrides$form.component) || 'form';
  var MyTitleTextInput = (overrides === null || overrides === void 0 || (_overrides$titleTextI = overrides.titleTextInput) === null || _overrides$titleTextI === void 0 ? void 0 : _overrides$titleTextI.component) || react.TextInput;
  var MyViewDescriptionContainer = (overrides === null || overrides === void 0 || (_overrides$viewDescri = overrides.viewDescriptionContainer) === null || _overrides$viewDescri === void 0 ? void 0 : _overrides$viewDescri.component) || 'p';
  var MyDefaultCheckbox = (overrides === null || overrides === void 0 || (_overrides$defaultChe = overrides.defaultCheckbox) === null || _overrides$defaultChe === void 0 ? void 0 : _overrides$defaultChe.component) || react.Checkbox;
  var MyPublicCheckbox = (overrides === null || overrides === void 0 || (_overrides$publicChec = overrides.publicCheckbox) === null || _overrides$publicChec === void 0 ? void 0 : _overrides$publicChec.component) || react.Checkbox;

  // A developer might override initial form values and not explicitly
  // assign all checkboxes a boolean value, but controlled Checkboxes
  // cannot have an undefined value.
  var enforceControlledCheckbox = function enforceControlledCheckbox(val) {
    return val !== undefined ? val : false;
  };
  return /*#__PURE__*/React__default.default.createElement(MyForm, _extends__default.default({
    "data-testid": testID,
    onSubmit: preventFormSubmission
  }, overrides === null || overrides === void 0 || (_overrides$form2 = overrides.form) === null || _overrides$form2 === void 0 ? void 0 : _overrides$form2.props), /*#__PURE__*/React__default.default.createElement(MyTitleTextInput, _extends__default.default({
    name: "title",
    "data-testid": "".concat(testID, "-title-input"),
    value: formValues.title || '',
    disabled: disabled,
    id: primaryInputId,
    invalid: titleInputInvalid,
    invalidText: titleInputInvalidText,
    labelText: titleInputLabelText,
    onChange: function onChange(evt) {
      _onChange({
        title: evt.target.value
      });
    },
    type: "text"
  }, overrides === null || overrides === void 0 || (_overrides$titleTextI2 = overrides.titleTextInput) === null || _overrides$titleTextI2 === void 0 ? void 0 : _overrides$titleTextI2.props)), /*#__PURE__*/React__default.default.createElement(MyViewDescriptionContainer, _extends__default.default({
    className: "".concat(iotPrefix, "--save-view-modal__view-description")
    // eslint-disable-next-line react/no-children-prop
    ,
    children: viewDescription
  }, overrides === null || overrides === void 0 || (_overrides$viewDescri2 = overrides.viewDescriptionContainer) === null || _overrides$viewDescri2 === void 0 ? void 0 : _overrides$viewDescri2.props)), /*#__PURE__*/React__default.default.createElement("fieldset", {
    className: "".concat(prefix, "--fieldset")
  }, /*#__PURE__*/React__default.default.createElement(MyDefaultCheckbox, _extends__default.default({
    name: "isDefault",
    "data-testid": "".concat(testID, "-default-checkbox"),
    checked: enforceControlledCheckbox(formValues.isDefault),
    disabled: disabled,
    id: "save-view-modal-default-checkbox-label",
    labelText: defaultCheckboxLabelText,
    onChange: function onChange(event, _ref2) {
      var checked = _ref2.checked;
      return _onChange({
        isDefault: checked
      });
    },
    wrapperClassName: "".concat(iotPrefix, "--save-view-modal__default-view-checkbox")
  }, overrides === null || overrides === void 0 || (_overrides$defaultChe2 = overrides.defaultCheckbox) === null || _overrides$defaultChe2 === void 0 ? void 0 : _overrides$defaultChe2.props)), /*#__PURE__*/React__default.default.createElement(MyPublicCheckbox, _extends__default.default({
    name: "isPublic",
    "data-testid": "".concat(testID, "-public-checkbox"),
    checked: enforceControlledCheckbox(formValues.isPublic),
    disabled: disabled,
    id: "save-view-modal-public-checkbox-label",
    labelText: publicCheckboxLabelText,
    wrapperClassName: "".concat(iotPrefix, "--save-view-modal__public-checkbox"),
    onChange: function onChange(event, _ref3) {
      var checked = _ref3.checked;
      return _onChange({
        isPublic: checked
      });
    }
  }, overrides === null || overrides === void 0 || (_overrides$publicChec2 = overrides.publicCheckbox) === null || _overrides$publicChec2 === void 0 ? void 0 : _overrides$publicChec2.props))));
};
TableSaveViewForm.propTypes = propTypes;
TableSaveViewForm.defaultProps = defaultProps;
TableSaveViewForm.__docgenInfo = {
  "description": "",
  "methods": [],
  "displayName": "TableSaveViewForm",
  "props": {
    "disabled": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "Disables the form elements when true",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "overrides": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "Used to overide the internal components and props for advanced customisation",
      "type": {
        "name": "shape",
        "value": {
          "form": {
            "name": "custom",
            "raw": "OverridePropTypes",
            "required": false
          },
          "titleTextInput": {
            "name": "custom",
            "raw": "OverridePropTypes",
            "required": false
          },
          "viewDescriptionContainer": {
            "name": "custom",
            "raw": "OverridePropTypes",
            "required": false
          },
          "defaultCheckbox": {
            "name": "custom",
            "raw": "OverridePropTypes",
            "required": false
          },
          "publicCheckbox": {
            "name": "custom",
            "raw": "OverridePropTypes",
            "required": false
          }
        }
      },
      "required": false
    },
    "primaryInputId": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "The id of the form elememt that should be initially focused",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "testID": {
      "defaultValue": {
        "value": "'TableSaveViewForm'",
        "computed": false
      },
      "description": "Id that can be used for testing",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "titleInputInvalid": {
      "defaultValue": {
        "value": "false",
        "computed": false
      },
      "description": "When true it will show that the title field has invalid input",
      "type": {
        "name": "bool"
      },
      "required": false
    },
    "titleInputInvalidText": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "Used to describe the input validation error for the the title field",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "viewDescription": {
      "defaultValue": {
        "value": "undefined",
        "computed": true
      },
      "description": "A text that describes what this view contains",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "formValues": {
      "description": "The state values of the controlled form elements e.g. { title: 'My view 1', isDefault: true }",
      "type": {
        "name": "objectOf",
        "value": {
          "name": "union",
          "value": [{
            "name": "string"
          }, {
            "name": "number"
          }, {
            "name": "bool"
          }]
        }
      },
      "required": true
    },
    "i18n": {
      "description": "Internationalisation strings",
      "type": {
        "name": "shape",
        "value": {
          "titleInputLabelText": {
            "name": "string",
            "required": false
          },
          "defaultCheckboxLabelText": {
            "name": "string",
            "required": false
          },
          "publicCheckboxLabelText": {
            "name": "string",
            "required": false
          }
        }
      },
      "required": true
    },
    "onChange": {
      "description": "Callback for when any of the form element's value changes",
      "type": {
        "name": "func"
      },
      "required": true
    }
  }
};
var TableSaveViewForm$1 = TableSaveViewForm;

module.exports = TableSaveViewForm$1;
