import get from 'lodash/get';
import set from 'lodash/set';
import toPath from 'lodash/toPath';
import { Vue } from 'vue-property-decorator';

export default function setReactive(obj: any, field: any, value?: any): void {
  const val = value ?? get(obj, field);
  const path = toPath(field);
  const isDeepPath = path.length > 1;

  if (isDeepPath) {
    const lastPathKey = path.pop() ?? '';
    set(obj, field, '');
    Vue.set(get(obj, path), lastPathKey, val);
  } else {
    delete obj[field];
    Vue.set(obj, field, val);
  }
}
