
function setAlign(sheet, headers) {
  const fuc = () => {
    headers.forEach(({ hAlign, vAlign }, index) => {
      sheetCommonObj.setAreaAlign(sheet.getRange(-1, index, -1, 1), hAlign, vAlign)
    });
  };
  sheetCommonObj.renderSheetFunc(sheet, fuc);
}

function setFormatter(sheet, headers) {
  const fuc = () => {
    headers.forEach(({ formatter }, index) => {
      if (formatter) {
        sheet.setFormatter(-1, index, formatter);
      }
    });
  };
  sheetCommonObj.renderSheetFunc(sheet, fuc);
}

function initSheet(dom, setting) {
  const workBook = sheetCommonObj.buildSheet(dom, setting);
  const sheet = workBook.getSheet(0);
  setAlign(sheet, setting.header);
  setFormatter(sheet, setting.header);
  return workBook;
}

function showData(sheet, data, headers, emptyRows) {
  const fuc = () => {
    sheet.setRowCount(data.length);
    data.forEach((item, row) => {
      headers.forEach(({ dataCode }, col) => {
        sheet.setValue(row, col, item[dataCode] || '');
      });
    });
    if (emptyRows) {
      sheet.addRows(data.length, emptyRows);
    }
  };
  sheetCommonObj.renderSheetFunc(sheet, fuc);
}

// 获取当前表中行数据
function getRowData(sheet, row, headers) {
  const item = {};
  headers.forEach(({ dataCode }, index) => {
    const value = sheet.getValue(row, index) || '';
    if (value) {
      item[dataCode] = value;
    }
  });
  return item;
}

// 获取表数据和缓存数据的不同数据
function getRowDiffData(curRowData, cacheRowData, headers) {
  let item = null;
  headers.forEach(({ dataCode }) => {
    const curValue = curRowData[dataCode];
    const cacheValue = cacheRowData[dataCode];
    if (!cacheValue && !curValue) {
      return;
    }
    if (cacheValue !== curValue) {
      if (!item) {
        item = {};
      }
      item[dataCode] = curValue || '';
    }
  });
  return item;
}

function getRowAllData(curRowData, headers) {
  let item = {};
  headers.forEach(({ dataCode }) => {
    const curValue = curRowData[dataCode];
    item[dataCode] = curValue || '';
  });
  return item;
}

const TIME_OUT = 1000 * 20;
const libID = window.location.search.match(/libID=([^&]+)/)[1];

const UpdateType = {
  UPDATE: 'update',
  DELETE: 'delete',
  CREATE: 'create',
};

const DEBOUNCE_TIME = 200;

const locked = lockUtil.getLocked();