import { TransCreateFn, TransferCtrl } from "..";

function getResize(this: TransferCtrl, direction: string) {
  const { transEvent, currComp } = this;
  const size = [...(currComp.layout.size || [])];
  if (direction.indexOf("x") != -1) {
    size[0] = transEvent.width + transEvent.offsetX * 2;
  }
  if (direction.indexOf("y") != -1) {
    size[1] = transEvent.height + transEvent.offsetY * 2;
  }
  return size;
}

const resize: TransCreateFn = (direction: string) => ({
  mousemove() {
    const style = this.module.helper.createStyle({
      size: getResize.call(this, direction),
    });
    Object.entries(style).forEach(([key, value]: any[]) => {
      this.compEl.style[key] = value;
    });
  },
  mouseup() {
    this.module.actions.updateCompData(
      this.currComp,
      "layout.size",
      getResize.call(this, direction)
    );
  },
});

export const resizeX = resize.bind(null, "x");
export const resizeY = resize.bind(null, "y");
export const resizeXY = resize.bind(null, "xy");
