'use strict';

require('core-js/modules/es.symbol.js');
require('core-js/modules/es.array.filter.js');
require('core-js/modules/es.object.get-own-property-descriptor.js');
require('core-js/modules/es.object.get-own-property-descriptors.js');
require('core-js/modules/es.object.keys.js');
require('core-js/modules/es.object.to-string.js');
require('core-js/modules/web.dom-collections.for-each.js');
var _extends = require('@babel/runtime/helpers/extends');
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var React = require('react');
var react = require('@carbon/react');
var PropTypes = require('prop-types');
var classnames = require('classnames');
var Settings = require('../../constants/Settings.js');
var Button = require('../Button/Button.js');
var deprecate = require('../../internal/deprecate.js');
var CodeEditor = require('../CodeEditor/CodeEditor.js');
var ComposedModal = require('../ComposedModal/ComposedModal.js');
var iconsReact = require('@carbon/icons-react');

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

var _extends__default = /*#__PURE__*/_interopDefault(_extends);
var _slicedToArray__default = /*#__PURE__*/_interopDefault(_slicedToArray);
var _objectWithoutProperties__default = /*#__PURE__*/_interopDefault(_objectWithoutProperties);
var _defineProperty__default = /*#__PURE__*/_interopDefault(_defineProperty);
var React__default = /*#__PURE__*/_interopDefault(React);
var PropTypes__default = /*#__PURE__*/_interopDefault(PropTypes);
var classnames__default = /*#__PURE__*/_interopDefault(classnames);

var _excluded = ["onSubmit", "onClose", "onCopy", "i18n", "language", "initialValue", "testID", "testId"];
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__default.default(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; }
var iotPrefix = Settings.settings.iotPrefix,
  prefix = Settings.settings.prefix;
var propTypes = _objectSpread(_objectSpread({}, ComposedModal.default.propTypes), {}, {
  // eslint-disable-line react/forbid-foreign-prop-types
  /*
   * On submit callback. It's called with editor value, and a callback to set an error messages
   * onSubmit(value, setError)
   */
  onSubmit: PropTypes__default.default.func.isRequired,
  /** Callback called when modal close icon or cancel button is pressed */
  onClose: PropTypes__default.default.func.isRequired,
  /** Callback called when editor copy icon is pressed */
  onCopy: PropTypes__default.default.func,
  /** All the labels that need translation */
  i18n: PropTypes__default.default.shape({
    errorTitle: PropTypes__default.default.string,
    modalTitle: PropTypes__default.default.string,
    modalLabel: PropTypes__default.default.string,
    modalHelpText: PropTypes__default.default.string,
    modalIconDescription: PropTypes__default.default.string,
    copyBtnDescription: PropTypes__default.default.string,
    copyBtnFeedBack: PropTypes__default.default.string,
    expandBtnLabel: PropTypes__default.default.string,
    modalPrimaryButtonLabel: PropTypes__default.default.string,
    modalSecondaryButtonLabel: PropTypes__default.default.string
  }),
  /** the language being written in the editor */
  language: PropTypes__default.default.string,
  /** Initial value for the editor */
  initialValue: PropTypes__default.default.string,
  // TODO: remove deprecated testID in v3.
  // eslint-disable-next-line react/require-default-props
  testID: deprecate(PropTypes__default.default.string, "The 'testID' prop has been deprecated. Please use 'testId' instead."),
  testId: PropTypes__default.default.string
});
var defaultProps = {
  i18n: {
    errorTitle: 'Error:',
    modalTitle: 'Edit the JSONs',
    modalLabel: 'JSON Editor',
    modalHelpText: null,
    modalIconDescription: 'Close',
    expandBtnLabel: 'Expand',
    modalPrimaryButtonLabel: 'Save',
    modalSecondaryButtonLabel: 'Cancel',
    copyBtnDescription: 'Copy to clipboard',
    copyBtnFeedBack: 'Copied!'
  },
  language: 'json',
  initialValue: null,
  onCopy: null,
  testId: 'card-code-editor'
};
var CardCodeEditor = function CardCodeEditor(_ref) {
  var onSubmit = _ref.onSubmit,
    onClose = _ref.onClose,
    onCopy = _ref.onCopy,
    i18n = _ref.i18n,
    language = _ref.language,
    initialValue = _ref.initialValue,
    testID = _ref.testID,
    testId = _ref.testId,
    composedModalProps = _objectWithoutProperties__default.default(_ref, _excluded);
  var _useState = React.useState(initialValue),
    _useState2 = _slicedToArray__default.default(_useState, 2),
    editorValue = _useState2[0],
    setEditorValue = _useState2[1];
  var _useState3 = React.useState(),
    _useState4 = _slicedToArray__default.default(_useState3, 2),
    isExpanded = _useState4[0],
    setIsExpanded = _useState4[1];
  var _useState5 = React.useState(false),
    _useState6 = _slicedToArray__default.default(_useState5, 2),
    error = _useState6[0],
    setError = _useState6[1];
  var mergedI18n = React.useMemo(function () {
    return _objectSpread(_objectSpread({}, defaultProps.i18n), i18n);
  }, [i18n]);
  var handleOnSubmit = function handleOnSubmit() {
    onSubmit(editorValue, setError);
  };
  var handleOnExpand = function handleOnExpand() {
    setIsExpanded(function (expandedState) {
      return !expandedState;
    });
  };
  return /*#__PURE__*/React__default.default.createElement(ComposedModal.default, _extends__default.default({
    "data-testid": "".concat(testId, "--card-code-editor-modal"),
    selectorPrimaryFocus: ".".concat(prefix, "--modal-header"),
    className: classnames__default.default("".concat(iotPrefix, "--editor"), _defineProperty__default.default({}, "".concat(iotPrefix, "--editor__expanded"), isExpanded)),
    isLarge: true,
    header: {
      label: mergedI18n.modalLabel,
      title: mergedI18n.modalTitle,
      helpText: mergedI18n.modalHelpText || null
    },
    onSubmit: handleOnSubmit,
    onClose: onClose,
    iconDescription: mergedI18n.modalIconDescription,
    footer: {
      primaryButtonLabel: mergedI18n.modalPrimaryButtonLabel,
      secondaryButtonLabel: mergedI18n.modalSecondaryButtonLabel
    }
  }, composedModalProps), /*#__PURE__*/React__default.default.createElement(Button, {
    className: "".concat(iotPrefix, "--editor-expand"),
    hasIconOnly: true,
    renderIcon: iconsReact.Popup,
    iconDescription: mergedI18n.expandBtnLabel,
    onClick: handleOnExpand,
    kind: "ghost"
    // TODO: remove deprecated testID in v3.
    ,
    testId: "".concat(testID || testId, "-expand-button")
  }), error && /*#__PURE__*/React__default.default.createElement(react.InlineNotification, {
    className: "".concat(iotPrefix, "--editor-notification"),
    kind: "error",
    onCloseButtonClick: function onCloseButtonClick() {
      return setError(false);
    },
    title: mergedI18n.errorTitle,
    subtitle: error
    // TODO: remove deprecated testID in v3.
    ,
    "data-testid": "".concat(testID || testId, "-notification")
  }), /*#__PURE__*/React__default.default.createElement(CodeEditor.default, {
    language: language,
    onCopy: onCopy,
    initialValue: initialValue,
    onCodeEditorChange: function onCodeEditorChange(value) {
      return setEditorValue(value);
    },
    light: true,
    i18n: mergedI18n,
    testId: testID || testId
  }));
};
CardCodeEditor.propTypes = propTypes;
CardCodeEditor.defaultProps = defaultProps;
CardCodeEditor.__docgenInfo = {
  "description": "",
  "methods": [],
  "displayName": "CardCodeEditor",
  "props": {
    "i18n": {
      "defaultValue": {
        "value": "{\n  errorTitle: 'Error:',\n  modalTitle: 'Edit the JSONs',\n  modalLabel: 'JSON Editor',\n  modalHelpText: null,\n  modalIconDescription: 'Close',\n  expandBtnLabel: 'Expand',\n  modalPrimaryButtonLabel: 'Save',\n  modalSecondaryButtonLabel: 'Cancel',\n  copyBtnDescription: 'Copy to clipboard',\n  copyBtnFeedBack: 'Copied!',\n}",
        "computed": false
      },
      "description": "All the labels that need translation",
      "type": {
        "name": "shape",
        "value": {
          "errorTitle": {
            "name": "string",
            "required": false
          },
          "modalTitle": {
            "name": "string",
            "required": false
          },
          "modalLabel": {
            "name": "string",
            "required": false
          },
          "modalHelpText": {
            "name": "string",
            "required": false
          },
          "modalIconDescription": {
            "name": "string",
            "required": false
          },
          "copyBtnDescription": {
            "name": "string",
            "required": false
          },
          "copyBtnFeedBack": {
            "name": "string",
            "required": false
          },
          "expandBtnLabel": {
            "name": "string",
            "required": false
          },
          "modalPrimaryButtonLabel": {
            "name": "string",
            "required": false
          },
          "modalSecondaryButtonLabel": {
            "name": "string",
            "required": false
          }
        }
      },
      "required": false
    },
    "language": {
      "defaultValue": {
        "value": "'json'",
        "computed": false
      },
      "description": "the language being written in the editor",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "initialValue": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "Initial value for the editor",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "onCopy": {
      "defaultValue": {
        "value": "null",
        "computed": false
      },
      "description": "Callback called when editor copy icon is pressed",
      "type": {
        "name": "func"
      },
      "required": false
    },
    "testId": {
      "defaultValue": {
        "value": "'card-code-editor'",
        "computed": false
      },
      "description": "",
      "type": {
        "name": "string"
      },
      "required": false
    },
    "onSubmit": {
      "description": "",
      "type": {
        "name": "func"
      },
      "required": true
    },
    "onClose": {
      "description": "Callback called when modal close icon or cancel button is pressed",
      "type": {
        "name": "func"
      },
      "required": true
    },
    "testID": {
      "description": "",
      "type": {
        "name": "custom",
        "raw": "deprecate(\n  PropTypes.string,\n  `The 'testID' prop has been deprecated. Please use 'testId' instead.`\n)"
      },
      "required": false
    }
  },
  "composes": ["../ComposedModal"]
};
var CardCodeEditor$1 = CardCodeEditor;

module.exports = CardCodeEditor$1;
