{
  "meta": {
    "instanceId": "7401306abfd0c80b5375eb53bf4b88f4ad2477de844b0b72628c8bdcaf1f6c07"
  },
  "nodes": [
    {
      "parameters": {},
      "id": "dc96d2d6-a896-4b65-ace4-72cbaa11164f",
      "name": "Start",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        -380,
        0
      ]
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "value": "1aFPqTTXVWLeDDz46loL5ARJQAYQvnLrugHZdGHuyel4",
          "mode": "list",
          "cachedResultName": "Google Maps Workflow",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1aFPqTTXVWLeDDz46loL5ARJQAYQvnLrugHZdGHuyel4/edit?usp=drivesdk"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "Job",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1aFPqTTXVWLeDDz46loL5ARJQAYQvnLrugHZdGHuyel4/edit#gid=0"
        },
        "options": {}
      },
      "id": "fc33081a-bab3-4265-803a-ab4a025da877",
      "name": "Get Data (Job)",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        -160,
        0
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "Vn2MJPj1Uv3svLzt",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// !!! ВАЖЛИВО: Впишіть сюди ТОЧНУ назву колонки з Google таблиці\nconst urlColumn = 'Website'; \n\nconst seenDomains = new Set();\nconst uniqueItems = [];\n\nif ($input.all().length === 0) return [];\n\nconst firstItem = $input.all()[0].json;\nif (firstItem[urlColumn] === undefined) {\n  const foundKey = Object.keys(firstItem).find(key => key.toLowerCase() === urlColumn.toLowerCase());\n  if (foundKey) {\n     throw new Error(`Помилка регістру! У коді написано '${urlColumn}', а в таблиці '${foundKey}'. Виправте код.`);\n  } else {\n     throw new Error(`Колонка '${urlColumn}' не знайдена! Доступні колонки: ${Object.keys(firstItem).join(', ')}`);\n  }\n}\n\nfor (const item of $input.all()) {\n  let url = item.json[urlColumn];\n  \n  if (url && typeof url === 'string') {\n    const urlMatch = url.match(/(?:q=)?(https?:\\/\\/[^&]+)/);\n    if (urlMatch && urlMatch[1]) {\n        url = decodeURIComponent(urlMatch[1]);\n        item.json[urlColumn] = url;\n    }\n\n    try {\n        const cleanDomain = url\n          .replace(/^(?:https?:\\/\\/)?(?:www\\.)?/i, \"\")\n          .split('/')[0]\n          .toLowerCase()\n          .trim();\n\n        if (!seenDomains.has(cleanDomain)) {\n          seenDomains.add(cleanDomain);\n          uniqueItems.push(item);\n        }\n    } catch (e) {\n        uniqueItems.push(item);\n    }\n  } else {\n    uniqueItems.push(item);\n  }\n}\n\nreturn uniqueItems;"
      },
      "id": "83f92fa7-8404-484e-b7a1-bb23c5993501",
      "name": "Code (Deduplicate)",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        60,
        0
      ]
    },
    {
      "parameters": {
        "operation": "clear",
        "documentId": {
          "__rl": true,
          "value": "1aFPqTTXVWLeDDz46loL5ARJQAYQvnLrugHZdGHuyel4",
          "mode": "list",
          "cachedResultName": "Google Maps Workflow",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1aFPqTTXVWLeDDz46loL5ARJQAYQvnLrugHZdGHuyel4/edit?usp=drivesdk"
        },
        "sheetName": {
          "__rl": true,
          "value": 1939296678,
          "mode": "list",
          "cachedResultName": "Result",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1aFPqTTXVWLeDDz46loL5ARJQAYQvnLrugHZdGHuyel4/edit#gid=1939296678"
        },
        "clear": "specificRange",
        "range": "A2:Z"
      },
      "id": "be8347e2-003a-471f-af4e-3b9257b04ed9",
      "name": "Clear Sheet (Result)",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        280,
        0
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "Vn2MJPj1Uv3svLzt",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "return $('Code (Deduplicate)').all();"
      },
      "id": "a1b2c3d4-e5f6-7890-1234-567890abcdef",
      "name": "Restore Data",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        500,
        0
      ]
    },
    {
      "parameters": {
        "batchSize": 500,
        "options": {}
      },
      "id": "f5e6d7c8-b9a0-1234-5678-90abcdef1234",
      "name": "Split In Batches",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        720,
        0
      ]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "1aFPqTTXVWLeDDz46loL5ARJQAYQvnLrugHZdGHuyel4",
          "mode": "list",
          "cachedResultName": "Google Maps Workflow",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1aFPqTTXVWLeDDz46loL5ARJQAYQvnLrugHZdGHuyel4/edit?usp=drivesdk"
        },
        "sheetName": {
          "__rl": true,
          "value": 1939296678,
          "mode": "list",
          "cachedResultName": "Result",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1aFPqTTXVWLeDDz46loL5ARJQAYQvnLrugHZdGHuyel4/edit#gid=1939296678"
        },
        "options": {}
      },
      "id": "acc0b94e-582c-4a8a-af2a-06cce3b16ebb",
      "name": "Append (Batch)",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        940,
        0
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "Vn2MJPj1Uv3svLzt",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "amount": 2,
        "unit": "seconds"
      },
      "id": "12345678-90ab-cdef-1234-567890abcdef",
      "name": "Wait 2s",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [
        1160,
        0
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Get Data (Job)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Data (Job)": {
      "main": [
        [
          {
            "node": "Code (Deduplicate)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code (Deduplicate)": {
      "main": [
        [
          {
            "node": "Clear Sheet (Result)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Clear Sheet (Result)": {
      "main": [
        [
          {
            "node": "Restore Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Restore Data": {
      "main": [
        [
          {
            "node": "Split In Batches",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split In Batches": {
      "main": [
        [
          {
            "node": "Append (Batch)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Append (Batch)": {
      "main": [
        [
          {
            "node": "Wait 2s",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait 2s": {
      "main": [
        [
          {
            "node": "Split In Batches",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}