import _cloneDeep from "lodash/cloneDeep";
import React from 'react';
import { connect } from "../../../redux";
import { IconButton } from '@webiny/ui/Button';
import { getContent } from "../../../selectors";
import { ReactComponent as SaveIcon } from "@svgr/webpack!../../../assets/icons/page-save-icon.svg";
import { buildCSSToString } from "../../../utils";

var PageSaveButton = function PageSaveButton(_ref) {
  var content = _ref.content,
      events = _ref.events,
      save = _ref.save,
      style = _ref.style;
  var _style$customStyle = style.customStyle,
      customStyle = _style$customStyle === void 0 ? {} : _style$customStyle,
      _style$temporaryStyle = style.temporaryStyle,
      temporaryStyle = _style$temporaryStyle === void 0 ? {} : _style$temporaryStyle,
      _style$pageStyle = style.pageStyle,
      pageStyle = _style$pageStyle === void 0 ? {} : _style$pageStyle,
      _style$currentTheme = style.currentTheme,
      currentTheme = _style$currentTheme === void 0 ? {} : _style$currentTheme;
  var name = currentTheme.name,
      url = currentTheme.url;

  var handleClick = function handleClick() {
    // save page
    var getContent = _cloneDeep(content);

    content.elements.map(function (v, index) {
      if (v.data.formInstance) {
        delete getContent.elements[index].data.formInstance;
      }
    }); // 编辑css加载到json

    var buldCSSCode = buildCSSToString(customStyle, temporaryStyle, pageStyle);
    getContent.css = buldCSSCode; // 加载theme

    if (name === 'dc') {
      getContent.theme = {
        name: name,
        url: url
      };
    }

    var pageJson = JSON.stringify(getContent); // const getForm = jsonToFromElement(getContent);
    // const pageElementListJson = {};
    // pageElementListJson.formElements = Object.keys(getForm).length > 0 ? getForm[content.elements[0].id] : [];
    // const pageElementListJsonNew = JSON.stringify(pageElementListJson);
    // const pageValueJson = {};
    // if (Object.keys(getForm).length > 0) {
    //   const getFormInstance = content.elements[0].data.formInstance;
    //   // getFormValue获取转换后的数据
    //   let values = getFormInstance.getFormValue();
    //   // 遍历，二次处理upload popEdit等组件
    //   values = handleValues(values, getContent);
    //   pageValueJson.formValue = { ...values };
    // } else {
    //   pageValueJson.formValue = {};
    //   // message.warn(i18n('PAGEBUIDER_SAVEPAGE_TIPS_NO_FORM'));
    // }
    // const pageValueJsonNew = JSON.stringify(pageValueJson);
    // save(pageJson, pageElementListJsonNew, events, pageValueJsonNew);

    save(pageJson, null, events, null);
  };

  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: "formbuild-btn-opt-default-color",
    onClick: function onClick() {
      return handleClick();
    }
  }, /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(IconButton, {
    icon: /*#__PURE__*/React.createElement(SaveIcon, null)
  }))));
};

export default connect(function (state) {
  var _state$appRender;

  return {
    content: getContent(state),
    events: (_state$appRender = state.appRender) === null || _state$appRender === void 0 ? void 0 : _state$appRender.events,
    style: state.style
  };
})(PageSaveButton);
//# sourceMappingURL=PageSaveButton.js.map