import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import React from "react";
import { i18n } from '@fishx/i18n';
import Action from "../../plugins/elementSettings/components/Action";
import { elementDefaultTargets, editorSettingKeyPrefix, PreviewBox } from "../../utils";
import { ElementGroup } from "../../../constant";
import PbPluginContainer from "./PbPluginContainer";
import PbPluginSettingContainer from "./PbPluginSettingContainer"; // 处理用户自定义组件

var pbElementPluginWrapper = function pbElementPluginWrapper(item) {
  var name = item.name,
      _item$group = item.group,
      group = _item$group === void 0 ? ElementGroup.CUSTOM : _item$group,
      _item$target = item.target,
      target = _item$target === void 0 ? elementDefaultTargets : _item$target,
      title = item.title,
      toolBarPreview = item.toolBarPreview,
      _item$data = item.data,
      data = _item$data === void 0 ? {} : _item$data,
      elements = item.elements,
      PluginComponent = item.pluginComponent,
      SettingComponent = item.pluginSettingComponent;
  var pluginSettings = [];
  var pluginSettingName; // 插件设置

  if (SettingComponent) {
    pluginSettingName = "".concat(editorSettingKeyPrefix, "-").concat(name);
    pluginSettings.push(pluginSettingName);
  }

  var plugins = [];
  var plugin = {
    name: "pb-editor-page-element-".concat(name),
    type: "pb-editor-page-element",
    elementType: name,
    toolbar: {
      title: title,
      group: group,
      preview: function preview() {
        var PreviewComponent;

        if (toolBarPreview) {
          PreviewComponent = toolBarPreview;
        }

        return /*#__PURE__*/React.createElement(PreviewBox, null, PreviewComponent ? PreviewComponent : /*#__PURE__*/React.createElement("span", null, name, " preview"));
      }
    },
    settings: pluginSettings,
    target: target,
    create: function create(options) {
      var children = [];

      if (Array.isArray(elements)) {
        children = elements;
      }

      if (typeof elements === 'function') {
        children = elements();
      }

      return _objectSpread({
        type: name,
        data: data,
        elements: children
      }, options);
    },
    render: function render(props) {
      return /*#__PURE__*/React.createElement(PbPluginContainer, Object.assign({}, props, {
        component: PluginComponent
      }));
    }
  };
  plugins.push(plugin);

  if (SettingComponent) {
    var settingPlugin = {
      name: pluginSettingName,
      type: "pb-editor-page-element-settings",
      renderAction: function renderAction() {
        return /*#__PURE__*/React.createElement(Action, {
          hideToolTip: true,
          plugin: this.name
        }, i18n('PAGEBUIDER_SETTINGS_ATTRS'));
      },
      renderMenu: function renderMenu() {
        return /*#__PURE__*/React.createElement(PbPluginSettingContainer, {
          component: SettingComponent
        });
      }
    };
    plugins.push(settingPlugin);
  }

  return plugins;
};

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