// JSON.parse(JSON.stringify(obj))
// structuredClone(obj)
// _.cloneDeep(obj);

const obj = {
  name: "Vasily",
  addres: {
    citi: "Saint",
    street: "Vilnizki",
  },
};

const obj2 = Object.assign({}, obj);
const obj3 = structuredClone(obj);

// console.log("shallow: ", obj.addres === obj2.addres);
// console.log("deepclone: ", obj.addres === obj3.addres);
//
// recursive implementation cloneDeep

function deepClone(value: any) {
  if (typeof value !== "object" || value === null) {
    return value;
  }

  if (Array.isArray(value)) {
    return value.map((item) => deepClone(item));
  }

  const objEntries = Object.entries(value).map(([key, value]) => [
    key,
    deepClone(value),
  ]);

  return Object.fromEntries(objEntries);
}

const obj4 = deepClone(obj);

console.log("deepclone(): ", obj.addres === obj4.addres);
