import {
  getElementId,
  randomCode,
  deepCopy,
  getNumList,
  objAssign,
} from "../../plugins/utils";

const MODEL = {
  type: "PAGE",
  columnGap: 20,
  locators: [],
  globals: [],
  columns: [],
  pageSize: "A3",
  columnNumber: 2,
  showForbidArea: false,
};
// 可编辑栏
const COLUMN = {
  type: "COLUMN",
  x: "",
  y: "",
  w: "",
  h: "",
  elements: [],
};
// 定位点
const LOCATOR = {
  type: "LOCATOR",
  x: "",
  y: "",
  w: "",
  h: "",
};

const getModel = (datas = {}) => {
  let npage = deepCopy(MODEL);
  npage = objAssign(npage, datas);
  npage.id = getElementId();
  const { pageSize, columnNumber } = npage;
  if (
    (pageSize === "A3" && columnNumber === 4) ||
    (pageSize === "A4" && columnNumber === 2)
  ) {
    npage.columnGap = 10;
  }

  const num = pageSize === "A3" ? 3 : 2;
  npage.locators = getNumList(num).map((item, index) => {
    const id = getElementId();
    return [
      {
        ...LOCATOR,
        id: `locator-${id}-${index}0`,
      },
      {
        ...LOCATOR,
        id: `locator-${id}-${index}1`,
      },
    ];
  });
  npage.columns = getNumList(columnNumber).map(() => {
    return { id: `column-${randomCode()}`, ...deepCopy(COLUMN) };
  });
  return npage;
};

export { MODEL, getModel };
