import _extends from "@babel/runtime/helpers/extends";
import { Panel } from '@cverse/core-lib-vue-beta';
export var ConnectorSchema = /*#__PURE__*/function () {
  function ConnectorSchema(componentName) {
    this.componentName = "Connector";
    this.componentName = componentName;
  }
  var _proto = ConnectorSchema.prototype;
  _proto.handlePanelViewJson = function handlePanelViewJson(viewJson) {
    var panel = new Panel();
    panel.load(viewJson);
    var attributeList = panel.getCurPageAttrListByWidgetKeyFromRoot("home");
    var nodeSchemaList = this.getNavigatorSchema();
    var nodeSchema = this.getOneSchemaByInstanceId(nodeSchemaList, viewJson.instanceId);
    this.setCurrentProps(nodeSchema, attributeList);
    this.resetAndRerenderSchema(nodeSchema);
  };
  _proto.getNavigatorSchema = function getNavigatorSchema() {
    var result = [];
    if (window && window.top) {
      var project = window.top['AliLowCodeEngine'].project;
      var schema = project.exportSchema();
      var schemaPage = Object.assign({}, schema.componentsTree[0]);
      result = this.getSchemaByComponentName(schemaPage, this.componentName);
    }
    return result;
  };
  _proto.resetAndRerenderSchema = function resetAndRerenderSchema(nodeSchema) {
    if (window && window.top) {
      var _project$simulatorHos;
      var project = window.top['AliLowCodeEngine'].project;
      var schema = project.exportSchema();
      var pageSchema = this.replaceSchemaBySchemaId(schema.componentsTree[0], nodeSchema);
      schema.componentsTree[0] = pageSchema;
      project.importSchema(schema);
      (_project$simulatorHos = project.simulatorHost) === null || _project$simulatorHos === void 0 ? void 0 : _project$simulatorHos.rerender();
    }
  };
  _proto.getSchemaByComponentName = function getSchemaByComponentName(schema, componentName) {
    var _this = this;
    var result = [];
    if (schema.componentName === componentName) {
      result.push(schema);
    }
    if (schema.children && schema.children.length) {
      schema.children.forEach(function (item) {
        result = result.concat(_this.getSchemaByComponentName(item, componentName));
      });
    }
    return result;
  };
  _proto.getOneSchemaByInstanceId = function getOneSchemaByInstanceId(nodeSchemaList, instanceId) {
    var result = null;
    nodeSchemaList.forEach(function (item) {
      if (item.props && item.props.entrySchema) {
        var _entrySchema$instance;
        var entrySchema = JSON.parse(item.props.entrySchema);
        if (((_entrySchema$instance = entrySchema.instance) === null || _entrySchema$instance === void 0 ? void 0 : _entrySchema$instance.id) === instanceId) {
          result = item;
        }
      }
    });
    return result;
  };
  _proto.setCurrentProps = function setCurrentProps(nodeSchema, attributeList) {
    if (nodeSchema !== null && nodeSchema !== void 0 && nodeSchema.props) {
      var currentProps = nodeSchema.props;
      var newProps = this.getPropsInAttributeList(attributeList);
      nodeSchema.props = _extends({}, currentProps, newProps);
    }
  };
  _proto.getPropsInAttributeList = function getPropsInAttributeList(attributeList) {
    var result = {};
    attributeList.forEach(function (item) {
      result[item.key] = item.value;
      if (item.choiceList && item.choiceList.length && item.value) {
        result[item.key] = item.value;
      }
    });
    return result;
  }

  /**
   * 根据id替换指定的schema
   * @param schema 
   * @param id 
   * @param newSchema 
   * @returns 
   */;
  _proto.replaceSchemaBySchemaId = function replaceSchemaBySchemaId(schema, newSchema) {
    var _this2 = this;
    if (schema.id === newSchema.id) {
      schema = newSchema;
    } else if (schema.children && schema.children.length) {
      schema.children = schema.children.map(function (item) {
        item = _this2.replaceSchemaBySchemaId(item, newSchema);
        return item;
      });
    }
    return schema;
  };
  return ConnectorSchema;
}();