import constants from "./constants";

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

  const {
    attrAccordion,
    classAccordion,
    selectorAccordion,
    attrAccordionContainer,
    accordionProps,
  } = constants;

  const attrKey = attrAccordion;
  const classKey = classAccordion;

  dc.addType(type, {
    model: linkModel.extend(
      {
        defaults: {
          ...linkModel.prototype.defaults,
          name: label,
          draggable: `[${attrAccordionContainer}]`,
          droppable: true,
          copyable: false,
          removable: false,
          selectable: true,
          ...accordionProps,
        },

        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({ [selectorAccordion]: "" });
          return cloned;
        },
      },
      {
        isComponent(el) {
          if (el.hasAttribute && el.hasAttribute(attrKey)) {
            return { type };
          }
        },
      }
    ),
    view: linkView,
  });
};
