import constants from "./constants";
export default (dc, label, type) => {
  const linkType = dc.getType("link");
  const linkModel = linkType.model;
  const linkView = linkType.view;

  const {
    attrTab,
    classTab,
    selectorTab,
    attrTabContainer,
    tabProps,
  } = constants;

  const attrKey = attrTab;
  const classKey = classTab;

  dc.addType(type, {
    model: linkModel.extend(
      {
        defaults: {
          ...linkModel.prototype.defaults,
          name: label,
          draggable: `[${attrTabContainer}]`,
          droppable: true,
          ...tabProps,
        },

        init() {
          const attrs = this.getAttributes();
          attrs[attrKey] = 1;
          this.setAttributes(attrs);
          classKey && this.addClass(classKey);
        },

        clone() {
          const cloned = linkModel.prototype.clone.apply(this, arguments);
          cloned.addAttributes({ [selectorTab]: "" });
          return cloned;
        },
      },
      {
        isComponent(el) {
          if (el.hasAttribute && el.hasAttribute(attrKey)) {
            return { type };
          }
        },
      }
    ),
    view: linkView,
  });
};
