local D=script:FindFirstAncestor("\084\101\120\116\069\100\105\116\111\114")local o=require(D.Packages.Roact)local m=require(D.Packages.RoactRodux)local c=require(D.Packages.Llama)local f=o.Component:extend("\065\112\112")local R=require(script.Parent.StudioThemeContext)local K=require(script.Parent.TextEditor)local C=require(script.Parent.TextLabel)function f.init(D)local m=(settings()).Studio D.labelText,D.updateLabelText=o.createBinding("")D.holderSize,D.updateHolderSize=o.createBinding(0)D:setState({theme=m.Theme})end function f.didMount(D)local o=(settings()).Studio D._themeConnection=o.ThemeChanged:Connect(function()D:setState({theme=o.Theme})end)end function f.willUnmount(D)D._themeConnection:Disconnect()end function f.willUpdate(m,D,o)if D.TextItem~=m.props.TextItem then m.updateLabelText(D.TextItem and D.TextItem.Text or"")end end function f.render(D)local m={}if D.props.TextItem then m.TextEditor=o.createElement(K,{updateHolderSize=D.updateHolderSize;pluginActions=D.props.pluginActions;labelText=D.labelText,updateLabelText=D.updateLabelText})else m.Label=o.createElement(C,{Text="\083\101\108\101\099\116 \097 \084\101\120\116\076\097\098\101\108\044 \084\101\120\116\066\117\116\116\111\110 \111\114 \084\101\120\116\066\111\120 \116\111 \101\100\105\116";AnchorPoint=Vector2.new(.5,.5),Position=UDim2.fromScale(.5,.5),TextWrapped=true;TextXAlignment=Enum.TextXAlignment.Center})end return o.createElement(R.Provider,{value=D.state.theme},{Holder=o.createElement(R.Consumer,{render=function(c)return o.createFragment({Layout=o.createElement("\085\073\076\105\115\116\076\097\121\111\117\116",{SortOrder=Enum.SortOrder.LayoutOrder});Holder=o.createElement("\083\099\114\111\108\108\105\110\103\070\114\097\109\101",{LayoutOrder=1;BackgroundColor3=c:GetColor(Enum.StudioStyleGuideColor.MainBackground,Enum.StudioStyleGuideModifier.Default),BorderColor3=c:GetColor(Enum.StudioStyleGuideColor.Border),Size=UDim2.fromScale(1,1),CanvasSize=D.holderSize:map(function(D)return UDim2.fromOffset(0,D)end),ScrollBarThickness=8,TopImage="\114\098\120\097\115\115\101\116\058\047\047\116\101\120\116\117\114\101\115\047\083\116\117\100\105\111\084\111\111\108\098\111\120\047\083\099\114\111\108\108\066\097\114\084\111\112\046\112\110\103";MidImage="\114\098\120\097\115\115\101\116\058\047\047\116\101\120\116\117\114\101\115\047\083\116\117\100\105\111\084\111\111\108\098\111\120\047\083\099\114\111\108\108\066\097\114\077\105\100\100\108\101\046\112\110\103",BottomImage="\114\098\120\097\115\115\101\116\058\047\047\116\101\120\116\117\114\101\115\047\083\116\117\100\105\111\084\111\111\108\098\111\120\047\083\099\114\111\108\108\066\097\114\066\111\116\116\111\109\046\112\110\103";VerticalScrollBarInset=Enum.ScrollBarInset.ScrollBar},m)})end})},D.props[o.Children])end return(m.connect(function(D,o)local m=c.Dictionary.copy(o)m.TextItem=D.TextItem m.TextXAlignment=D.TextXAlignment return m end))(f)
local TextEditor = script:FindFirstAncestor("TextEditor")
local Roact = require(TextEditor.Packages.Roact)
local RoactRodux = require(TextEditor.Packages.RoactRodux)
local Llama = require(TextEditor.Packages.Llama)

local App = Roact.Component:extend("App")
local StudioThemeContext = require(script.Parent.StudioThemeContext)
local TextEditorComponent = require(script.Parent.TextEditor)
local ThemedTextLabel = require(script.Parent.TextLabel)

function App:init()
  local studioSettings = settings().Studio

  self.labelText, self.updateLabelText = Roact.createBinding("")
  self.holderSize, self.updateHolderSize = Roact.createBinding(0)
  self:setState({ 
    theme = studioSettings.Theme
  })
end

function App:didMount()
  local studioSettings = settings().Studio
  self._themeConnection = studioSettings.ThemeChanged:Connect(function()
    self:setState({ 
      theme = studioSettings.Theme
    })
  end)
end

function App:willUnmount()
  self._themeConnection:Disconnect()
end

function App:willUpdate(newProps, _newState)
  if newProps.TextItem ~= self.props.TextItem then
    self.updateLabelText(newProps.TextItem and newProps.TextItem.Text or "")
  end
end

function App:render()
  local Children = {}

  if self.props.TextItem then
    Children.TextEditor = Roact.createElement(TextEditorComponent, {
      updateHolderSize = self.updateHolderSize,
      pluginActions = self.props.pluginActions,
      labelText = self.labelText,
      updateLabelText = self.updateLabelText,
    })
  else
    Children.Label = Roact.createElement(ThemedTextLabel, {
      Text = "Select a TextLabel, TextButton or TextBox to edit",
      AnchorPoint = Vector2.new(0.5, 0.5),
      Position = UDim2.fromScale(0.5, 0.5),
      TextWrapped = true,
      TextXAlignment = Enum.TextXAlignment.Center,
    })
  end

  return Roact.createElement(StudioThemeContext.Provider, {
    value = self.state.theme
  }, {
    Holder = Roact.createElement(StudioThemeContext.Consumer, {
      render = function(theme)
        return Roact.createFragment({
          Layout = Roact.createElement("UIListLayout", {
            SortOrder = Enum.SortOrder.LayoutOrder,
          }),

          Holder = Roact.createElement("ScrollingFrame", {
            LayoutOrder = 1,
            BackgroundColor3 = theme:GetColor(Enum.StudioStyleGuideColor.MainBackground, Enum.StudioStyleGuideModifier.Default),
            BorderColor3 = theme:GetColor(Enum.StudioStyleGuideColor.Border),
            Size = UDim2.fromScale(1, 1), -- self.props.TextItem and UDim2.new(1, 0, 1, -35) or UDim2.fromScale(1, 1),
            CanvasSize = self.holderSize:map(function(value)
              return UDim2.fromOffset(0, value)
            end),
            ScrollBarThickness = 8,
            TopImage = "rbxasset://textures/StudioToolbox/ScrollBarTop.png",
            MidImage = "rbxasset://textures/StudioToolbox/ScrollBarMiddle.png",
            BottomImage = "rbxasset://textures/StudioToolbox/ScrollBarBottom.png",
            -- ScrollBarImageColor3 = theme:GetColor(Enum.StudioStyleGuideColor.ScrollBar), TODO: this colour is really bad...
            VerticalScrollBarInset = Enum.ScrollBarInset.ScrollBar,
          }, Children),
        })
      end
    })
  }, self.props[Roact.Children])
end

return RoactRodux.connect(
  function(state, props)
    local newProps = Llama.Dictionary.copy(props)
    newProps.TextItem = state.TextItem
    newProps.TextXAlignment = state.TextXAlignment
    return newProps
  end
)(App)