local j=game:GetService("\072\116\116\112\083\101\114\118\105\099\101")local E=script:WaitForChild("\109\111\100\117\108\101\115")local T=(E:FindFirstChild("\076\111\097\100\101\114\085\116\105\108\115",true)).Parent local e=(require(T)).bootstrapPlugin(E)local q=e("\077\097\105\100")local p=e("\077\097\116\101\114\105\097\108\073\110\112\117\116")local o=e("\084\101\114\114\097\105\110\067\111\110\118\101\114\116\101\114")local B=e("\080\108\117\103\105\110\085\073")local x=e("\083\101\114\118\105\099\101\066\097\103")local G=plugin local f=script:IsDescendantOf(game)local W if f then warn("\083\116\097\114\116\105\110\103 \112\108\117\103\105\110 \105\110 \100\101\098\117\103 \109\111\100\101")G=(e("\080\108\117\103\105\110\070\097\099\097\100\101")).new(G)W=(e("\083\101\108\101\099\116\105\111\110\070\097\099\097\100\101")).new(G)else W=game.Selection end local k=script.Parent:WaitForChild("\081\117\101\110\116\121\080\097\114\116\084\111\084\101\114\114\097\105\110\083\099\114\101\101\110\071\117\105")k.Enabled=false local a=q.new()local m=false local function N(j)if not m then return end m=false a._current=nil end local function Z(E)if m then return end m=true local T=q.new()local Z=T:Add(x.new())Z:GetService(e("\080\108\117\103\105\110\084\101\109\112\108\097\116\101\080\114\111\118\105\100\101\114"))Z:Init()Z:Start()local g=T:Add(o.new(Z))local U=T:Add(k:Clone())U.Enabled=true U.Parent=f and game.Players.LocalPlayer.PlayerGui or game.CoreGui local V=B.new(Z,U.Main,W,g)T:GiveTask(V)T:GiveTask(V.RequestClose:Connect(function()N()end))T:GiveTask(V.RequestConvert:Connect(function(j,E)g:Convert(j,E)end))local u=p.new(G:GetMouse())V:SetMaterialInput(u)if not f then local E=game:GetService("\067\104\097\110\103\101\072\105\115\116\111\114\121\083\101\114\118\105\099\101")T:GiveTask(g.ConversionStarting:Connect(function()E:SetWaypoint("\067\111\110\118\101\114\115\105\111\110\095"..j:GenerateGUID(true))end))end if E then E:SetActive(true)T:GiveTask(function()E:SetActive(false)end)end a._current=T end if f then Z()else local j=G:CreateToolbar("\079\098\106\101\099\116")local E=j:CreateButton("\080\097\114\116 \116\111 \084\101\114\114\097\105\110","\067\111\110\118\101\114\116\115 \114\111\098\108\111\120 \112\097\114\116\115 \116\111 \116\101\114\114\097\105\110","\114\098\120\097\115\115\101\116\105\100\058\047\047\049\054\049\056\049\054\056\052\050\050")E.Click:Connect(function()if not m then Z(E)else N(E)end end)G.Unloading:Connect(function()N(E)end)end
--[=[
	Part to terrain plugin
	@class PartToTerrainPlugin
]=]

local HttpService = game:GetService("HttpService")

local modules = script:WaitForChild("modules")
local loader = modules:FindFirstChild("LoaderUtils", true).Parent

local require = require(loader).bootstrapPlugin(modules)

local Maid = require("Maid")
local MaterialInput = require("MaterialInput")
local TerrainConverter = require("TerrainConverter")
local PluginUI = require("PluginUI")
local ServiceBag = require("ServiceBag")

-- luacheck: push ignore
local plugin = plugin
-- luacheck: pop ignore

local IS_DEBUG_MODE = script:IsDescendantOf(game)

local selectionService
if IS_DEBUG_MODE then
	warn("Starting plugin in debug mode")
	plugin = require("PluginFacade").new(plugin)
	selectionService = require("SelectionFacade").new(plugin)
else
	selectionService = game.Selection
end

local screenGui = script.Parent:WaitForChild("QuentyPartToTerrainScreenGui")
screenGui.Enabled = false

local mainMaid = Maid.new()

local isActive = false
local function deactivate(_button)
	if not isActive then
		return
	end

	isActive = false
	mainMaid._current = nil
end

local function activate(button)
	if isActive then
		return
	end
	isActive = true
	local maid = Maid.new()
	local serviceBag = maid:Add(ServiceBag.new())
	serviceBag:GetService(require("PluginTemplateProvider"))

	serviceBag:Init()
	serviceBag:Start()


	local converter = maid:Add(TerrainConverter.new(serviceBag))
	local newScreenGui = maid:Add(screenGui:Clone())

	newScreenGui.Enabled = true
	newScreenGui.Parent = IS_DEBUG_MODE and game.Players.LocalPlayer.PlayerGui or game.CoreGui

	local pluginUI = PluginUI.new(serviceBag, newScreenGui.Main, selectionService, converter)
	maid:GiveTask(pluginUI)
	maid:GiveTask(pluginUI.RequestClose:Connect(function()
		deactivate()
	end))

	maid:GiveTask(pluginUI.RequestConvert:Connect(function(selection, material)
		converter:Convert(selection, material)
	end))

	local materialInput = MaterialInput.new(plugin:GetMouse())
	pluginUI:SetMaterialInput(materialInput)

	-- Handle change history service
	if not IS_DEBUG_MODE then
		local ChangeHistoryService = game:GetService("ChangeHistoryService")
		maid:GiveTask(converter.ConversionStarting:Connect(function()
			ChangeHistoryService:SetWaypoint("Conversion_" .. HttpService:GenerateGUID(true))
		end))
	end

	-- Handle plugin button
	if button then
		button:SetActive(true)
		maid:GiveTask(function()
			button:SetActive(false)
		end)
	end

	mainMaid._current = maid
end

if IS_DEBUG_MODE then
	activate()
else
	local toolbar = plugin:CreateToolbar("Object")
	local button = toolbar:CreateButton(
		"Part to Terrain",
		"Converts roblox parts to terrain",
		"rbxassetid://1618168422"
	)

	button.Click:Connect(function()
		if not isActive then
			activate(button)
		else
			deactivate(button)
		end
	end)

	plugin.Unloading:Connect(function()
		deactivate(button)
	end)
end