import { computed, shallowRef } from "vue";
import { ContentApis, ContentConfig } from "/@/components/PageContent";
import { FormConfig } from "/@/components/PageSearch";
import { cloneDeep } from "lodash";

interface InsertBusinessField {
  index?: number;
  name?: string;
  code?: string;
}

interface IContentOptions {
  apis: ContentApis;
  sourceConfig: ContentConfig;
  insertBusinessField?: InsertBusinessField;
}

const defaultInsertBusinessField: InsertBusinessField = {
  index: 2,
  name: "relaComName",
  code: "relaComNo"
};

export function useBusinessContent(options: IContentOptions) {
  const {
    sourceConfig,
    apis,
    insertBusinessField = defaultInsertBusinessField
  } = options;
  const contentConfig = cloneDeep(sourceConfig) as ContentConfig;
  const { columns: beforeColumns } = contentConfig;

  const {
    index = defaultInsertBusinessField.index,
    name = defaultInsertBusinessField.name,
    code = defaultInsertBusinessField.code
  } = insertBusinessField;

  const afterColumns = beforeColumns.splice(index);

  contentConfig.apis = apis;
  contentConfig.columns = beforeColumns.concat(afterColumns);

  return {
    contentConfig
  };
}
