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 PluginContainer from "./PluginContainer";
import PluginSettingContainer from "./PluginSettingContainer";
import { getCustomPluginJson } from "./util"; // 处理用户自定义组件

var elementPluginWrapper = function elementPluginWrapper(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,
      PluginComponent = item.pluginComponent,
      SettingComponent = item.pluginSettingComponent;
  var pluginSettings = ["".concat(editorSettingKeyPrefix, "-cascade-set"), "".concat(editorSettingKeyPrefix, "-style-set")];
  var pluginSettingName; // 插件设置

  if (SettingComponent) {
    pluginSettingName = "".concat(editorSettingKeyPrefix, "-").concat(name);
    pluginSettings.unshift(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"));
      },
      toolBarPreview: toolBarPreview
    },
    settings: pluginSettings,
    target: target,
    create: function create() {
      var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      return getCustomPluginJson(item, options);
    },
    render: function render(props) {
      return /*#__PURE__*/React.createElement(PluginContainer, 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() {
        var ElementSettingComponent = SettingComponent;

        if (!Array.isArray(SettingComponent)) {
          ElementSettingComponent = [SettingComponent];
        }

        return /*#__PURE__*/React.createElement(PluginSettingContainer, {
          components: ElementSettingComponent
        });
      }
    };
    plugins.push(settingPlugin);
  }

  return plugins;
};

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